The javadoc section for Formatting and Analysis Templates for DateTimeFormatter lists the following 3 corresponding characters:
Symbol Meaning Presentation Examples ------ ------- ------------ ------- G era text AD; Anno Domini; A u year year 2004; 04 y year-of-era year 2004; 04
Just for comparison, these other characters are easy enough to understand:
D day-of-year number 189 d day-of-month number 10 E day-of-week text Tue; Tuesday; T
day-of-year , day-of-month and day-of-week - this is obviously the day within this area (year, month, week).
So, year-of-era means the year within a given area (era), and right above it, era displayed with an approximate value of AD (another value, which, of course, is BC ).
year - the signed year, where year 0 is 1 BC , year -1 is 2 BC , etc.
To illustrate: When was the Killer Julius Caesar ?
- March 15, 44 BC (using the
MMMM d, y GG template) - March 15, -43 (using the
MMMM d, u pattern)
Of course, the difference will only matter if the year is zero or negative, and since this is rare, most people donβt care, although they should.
Conclusion: If you use y , you should also use G Since G rarely used, the correct character is the year u , not y , otherwise a non-positive year will not display correctly.
This is called defensive programming :
Defensive programming is a form of defensive design designed to provide continuous function of a piece of software in unforeseen circumstances .
Note that DateTimeFormatter corresponds to SimpleDateFormat :
Letter Date or Time Component Presentation Examples ------ ---------------------- ------------ -------- G Era designator Text AD y Year Year 1996; 96
Negative years have always been a problem, and now they have fixed it by adding u .