LocalDate date = LocalDate.now();
System.out.println("date :" + date );//default format is yyyy-MM-dd
System.out.println(date.getClass().getName());//java.time.LocalDate
How to format the above date
in LocalDate
type with the format
dd-MM-yyyy
. But you can use String date pattern i.e dd-MM-yyyy
. The output should be of LocalDate
type only.
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.