Save a formatted String to a LocalDate

半世苍凉 提交于 2019-12-02 07:18:32

You cannot. A LocalDate does not have and cannot have a format in it.

If you adhere to separation of model and presentation, it would also be incorrect to put the format into the date. The LocalDate is part of your model (I presume). The EEE, dd. MM. uuuu format belongs in your presentation. Your converter class bridges the two.

A LocalDate holds a value, a date in the calendar, nothing else. Much the same way as an int holds a value. For example, an int may hold the value 64458. For presentation you may format it into strings like 000000064458, +64458 or 64,458 or even 64458.00 or in hex. The int stays the same. In the same way your LocalDate stays the same no matter which formatting operations you do. You can have your desired format only in a String outside the LocalDate.

As a compromise you may fit your class with a getFormattedDate method that formats your date into a string. You decide whether this would blur the separation between model and presentation too much, or since the date should always be formatted, in this case it’s acceptable to you.

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