Home >Java >javaTutorial >SimpleDateFormat: Why Does 'Y' Show a Different Year Than 'y'?
In SimpleDateFormat, Why Does 'Y' Return 2012 While 'y' Returns 2011?
The behavior you observed, where 'Y' prints 2012 and 'y' prints 2011 for the same Date object, is caused by the subtle distinction between the week year and the calendar year.
Week Year vs. Calendar Year
In date formatting, 'Y' represents the week year, which is aligned with the calendar year, but can sometimes be different due to the way weeks are calculated. The week year starts on the Sunday of the first week of the year, and ends on the Saturday of the last week of the year.
On the other hand, 'y' represents the calendar year, which is the traditional Gregorian calendar year that starts on January 1st.
In your example, the Date object you used falls within the first week of 2012 according to the week year calculation, but it falls within the last week of 2011 according to the calendar year calculation. As a result, 'Y' prints 2012 (the week year) and 'y' prints 2011 (the calendar year).
To further illustrate this concept, consider the following example:
System.out.println(new SimpleDateFormat("Y").format(new Date("December 29, 1997"))); // prints 1998 System.out.println(new SimpleDateFormat("y").format(new Date("December 29, 1997"))); // prints 1997
In this case, 'Y' prints 1998 because the date falls within the first week of that week year, even though it's technically the last week of 1997. 'y', however, prints 1997 because that's the calendar year for the given date.
The above is the detailed content of SimpleDateFormat: Why Does 'Y' Show a Different Year Than 'y'?. For more information, please follow other related articles on the PHP Chinese website!