How to format LocalDate object to MM/dd/yyyy and have format persist

后端 未结 5 1186
自闭症患者
自闭症患者 2020-11-27 07:37

I am reading text and storing the dates as LocalDate variables.

Is there any way for me to preserve the formatting from DateTimeFormatter so that when I call the Lo

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 08:04

    Short answer: no.

    Long answer: A LocalDate is an object representing a year, month and day, and those are the three fields it will contain. It does not have a format, because different locales will have different formats, and it will make it more difficult to perform the operations that one would want to perform on a LocalDate (such as adding or subtracting days or adding times).

    The String representation (produced by toString()) is the international standard on how to print dates. If you want a different format, you should use a DateTimeFormatter of your choosing.

提交回复
热议问题