I want to convert date from String to LocalDate object: Starting date as String: "12/11/2022" I want to convert this String to LocalDate. I want to get LocalDate object and next use this LocalData inside if-else conditions to convert this object to String and return String. All conditions return first LocalData and next String as result: if I use this code with: date = LocalDate.parse(default)};
public String getStringDate(String timeProjection){
if (timeProjection.equals("1") {
date = LocalDate.now();
}
....
else {
String default = "12/11/2022";
date = LocalDate.parse(default);
}
command
return date.format(DataTimeFormatter.ofPattern("dd/MM/YYYY"));
I get issue that is impossible this parse, but when I use:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
This convertion works good. What do you think is possible create solution when I can't use twice this formatter: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY") - first in else body, second in result command? Thanks for your help.
DateTimeParseException: Text '16/08/2016' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=16, WeekBasedYear[WeekFields[SUNDAY,1]]=2016, MonthOfYear=8},ISO of type java.time.format.Parsed. This is because upper caseYYYYis wrong. Use either lower caseuuuuor lower caseyyyy. In yourreturnstatement too.date = LocalDate.of(2022, Month.NOVEMBER, 12);.