How can you make LocalDate to return Date in a specific format?

♀尐吖头ヾ 提交于 2020-06-29 06:21:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!