-3
I was taking a date in a jLabel in the format dd/MM/yyyy and trying to convert it to Localdate and then use it with Localdate methods, but I ran into some errors and found nothing on the internet that could solve.
Below is the correct way to convert string to Localdate.
/*
Convertendo a string em LocalDate no formato padrão (yyyy-MM-dd). O método ofPattern deve ter a mascara que esta sendo utilizado no source (jLabelDia), então, ao converter em LocalDate, o formato automaticamente passa a ser iso (padrão do LocalDate yyyy-MM-dd), então vc pode trabalhar com os métodos da LocalDate.
*/
String data = jLabelDia.getText();
LocalDate ld = LocalDate.parse(data, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
String dia = String.valueOf(ld.minusDays(1));
jLabelDia.setText(dia);
The format that localDate works by default is yyyy-MM-dd. The mask you use in the method of.Pattern is the mask that is being used in the souce, in this case, the jLabelDia that has the dd/MM/yyyy mask as informed at the beginning of the question.
– Alexandre Bezerra
corrected title.
– Alexandre Bezerra
It should work, see: https://ideone.com/xbeMQx - my guess is
jLabelDia.getText()
is returning something else (maybe with a space or another "invisible" character at the beginning)– hkotsubo