0

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 .

Sam
  • 7
  • 4
  • `formator.parse` returns `date/time`, `formator.format` return string. – xingbin Nov 24 '18 at 06:29
  • Possible duplicate of [Localdate.format, format is not applied](https://stackoverflow.com/questions/50462041/localdate-format-format-is-not-applied) – Ole V.V. Nov 26 '18 at 16:54
  • If you can explain why you think you want that, we can likely find a working solution for you. Basically you shouldn’t want a date with a format. Could this be an [XY problem](https://en.wikipedia.org/wiki/XY_problem)? – Ole V.V. Nov 26 '18 at 16:57

1 Answers1

2

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.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Very nice explanation , i just wanted to confirm it . if Either way it could be possible it would have been better , but if it is not , we have to find other way around. – Sam Nov 30 '18 at 03:04