This feature is not a responsibility of LocalDate
class which is an immutable date-time object that represents a date. Its duty is not to care about the String format representation.
To generate or parse strings, use the DateTimeFormatter
class.
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String string = date.format(pattern);
Back to LocalDate
, use the same pattern:
LocalDate dateParsed = LocalDate.parse(string, pattern);
But the new dateParsed
will again be converted to its default String representation since LocalDate
overrides toString()
method. Here is what the documentation says:
The output will be in the ISO-8601 format uuuu-MM-dd.
You might want to implement your own decorator of this class which handles the formatting.