Why Does Java's Java.Time.Format.Datetimeformatter#Format(Localdatetime) Add a Year

Why does Java's java.time.format.DateTimeFormatter#format(LocalDateTime) add a year?

From Wikipedia:

[YYYY] indicates the ISO week-numbering year which is slightly
different from the traditional Gregorian calendar year (see below).

  1. YYYY is an ISO-8601 style representation of the year.
  2. yyyy is the Gregorian year-of-era representation.

Since the the calculation of the two can be different by +1 or -1, hence the formatting. More useful info at YEAR_OF_ERA, YEAR, and weekBasedYear.

DateTimeFormatter adding a year to date after formatting

From the DateTimeFormatter documentation:, indicating the symbol, meaning and examples:

Y week-based-year year 1996; 96

So you're formatting the week-based-year, not the regular year. December 30th 2019 belongs to the first week of 2020, hence your output.

Use yyyy (year-of-era) or uuuu (year) instead of YYYY and you'll get 2019 instead.

Basically, YYYY should usually be used with w (week-of-week-based-year) and E (day-of-week).

Formatting Java Instant to YYYYMMdd adds an extra year?

You want lowercase y -

        System.out.println(
DateTimeFormatter.ofPattern("yyyyMMdd")
.withZone(ZoneId.of("UTC"))
.format(Instant.parse("2020-12-31T08:00:00Z"))
);

clojure java-time - date moves forward a year?

Because yyyy is not YYYY

From Oracle docs for DateTimeFormatter:

 ... snip...
u year year 2004; 04
y year-of-era year 2004; 04
...snip...
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
...snip...

I'm not very proficient at datetime arithmetic or the nuances but I believe that in week-based year counting this December's 30th and 31st are counted as year 2020 because these dates belong to week #1, not week #52 or #53.

Bug in java LocalDate for Date Format

Your format is incorect if you are expecting 2020 for both dates. Change it to yyyyMMdd

  • y (lowercase) is year
  • Y (uppercase) is 'week-based-year'

Worth a read: yyyy-vs-yyyy-the-day-the-java-date-formatter-hurt-my-brain

How can I parse/format dates with LocalDateTime? (Java 8)

Parsing date and time

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

Java ZonedDateTime.format issue

Change the pattern string to yyyy-MM-dd.
Refer to the javadoc for class DateTimeFormatter to understand the difference between YYYY and yyyy.

ZonedDateTime z1 = ZonedDateTime.of(LocalDateTime.of(2020, 12, 31, 0, 0), ZoneId.of("America/New_York"));
System.out.println(z1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

Result:

2020-12-31


Related Topics



Leave a reply



Submit