问题
How can you make LocalDate to return Date in a specific format ?
Ex . LocalDate ld = LocalDate.now();
The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 .
** Dont say use formatter because formatter will not return date , it will return String which is of no use for me .
回答1:
Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A date will never have only one accepted format. So yes, for representing a date as string, users should apply any customized formatter.
But I suspect that you want to change the behaviour of the LocalDate
-method toString()
which produces a string in the ISO-format "2018-11-24". Well, you cannot because the class LocalDate
is final so overriding this method is impossible, and there is also no configuration hook to change the behaviour because this would be in conflict with the immutability of the class.
来源:https://stackoverflow.com/questions/53455589/how-can-you-make-localdate-to-return-date-in-a-specific-format