The DateTimeFormatter
class documentation says about its formatting codes for the year:
u year year 2004; 04
y year-of-era year 2004; 04
…
Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive. If the count of letters is less than four (but not two), then the sign is only output for negative years as per SignStyle.NORMAL. Otherwise, the sign is output if the pad width is exceeded, as per SignStyle.EXCEEDS_PAD.
No other mention of “era”.
So what is the difference between these two codes, u
versus y
, year
versus year-of-era
?
When should I use something like this pattern uuuu-MM-dd
and when yyyy-MM-dd
when working with dates in Java?
Seems that example code written by those in the know use uuuu
, but why?
Other formatting classes such as the legacy SimpleDateFormat
have only yyyy
, so I am confused why java.time brings this uuuu
for “year of era”.