问题
In a need to convert Java LocalDate
of the format of dd-MM-yyyy
into a LocalDate
of dd/MM/yyyy
.
Trying with :
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = // LocalDate value in dd-MM-yyyy format
String stringDate = dateFormat.format(date);
LocalDate convertedDate = LocalDate.parse(stringDate, dateFormat);
But still it resulting into return a date in dd-MM-yyyy
format. Any efficient way to do this?
回答1:
The default toString implementation in LocalDate.java seems to be hardwired with '-' as a separator. So all default print statements will result into same format. Seems only way out would be to use a formatter and get a string output.
return buf.append(monthValue < 10 ? "-0" : "-")
.append(monthValue)
.append(dayValue < 10 ? "-0" : "-")
.append(dayValue)
.toString();
Also, purpose of LocalDate.parse(..) is not to convert the date format. It's actually meant to just get a date value in String and give a resulting LocalDate instance.
Hope this helps.
来源:https://stackoverflow.com/questions/61339860/how-to-change-localdate-format-resulting-into-a-localdate-without-resulting-into