Save a formatted String to a LocalDate

前端 未结 1 1690
名媛妹妹
名媛妹妹 2021-01-23 11:32

I have a DateConverter class:

public class DateConverter extends StringConverter {

    String pattern = \"EEE, dd. MM. uuuu\";
            


        
相关标签:
1条回答
  • 2021-01-23 12:18

    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.

    0 讨论(0)
提交回复
热议问题