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

后端 未结 5 1198
自闭症患者
自闭症患者 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:05

    Just format the date while printing it out:

    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
        String text = date.format(formatters);
        LocalDate parsedDate = LocalDate.parse(text, formatters);
    
        System.out.println("date: " + date);
        System.out.println("Text format " + text);
        System.out.println("parsedDate: " + parsedDate.format(formatters));
    }
    

提交回复
热议问题