I am trying to take a date formatted String, parse it to LocalDateTime, then format it back to a String for display. What I found when doing this is that the original string is year 2020 and the LocalDateTime object also has the year 2020. But when I format the LocalDateTime for display, this String somehow is now year 2021? Here's the code:
public class LocalDateTimeTry {
public static void main(String[] args) {
String originalData = "2020-12-29T20:01:06+0000";
DateTimeFormatter originalFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxxx");
LocalDateTime originalParsed
= LocalDateTime.parse(originalData, originalFormatter);
DateTimeFormatter displayFormatter
= DateTimeFormatter.ofPattern("MMMM d, YYYY");
String displayFormatted
= displayFormatter.format(originalParsed);
System.out.printf("Original String of data : %s%n", originalData);
System.out.printf("LocalDateTime.toString(): %s%n", originalParsed.toString());
System.out.printf("Formatted for display : %s%n", displayFormatted);
}
}
The output looks like this: See how the year changes from 2020 to 2021.
Original String of data : 2020-12-29T20:01:06+0000
LocalDateTime.toString(): 2020-12-29T20:01:06
Formatted for display : December 29, 2021
Any thoughts??
question from:https://stackoverflow.com/questions/66065188/localdatetime-formatter-changing-year-from-2020-to-2021