The DateTimeFormatter
class documentation defines separate symbols u
for year and y
year-of-era: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns
What is the difference between year and year-of-era?
In a week-based-year, each week belongs to only a single year. Week 1 of a year is the first week that starts on the first day-of-week and has at least the minimum number of days. The first and last weeks of a year may contain days from the previous calendar year or next calendar year respectively.
While a lower-case yyyy is the year, an upper-case YYYY is the week year. The current week year is usually the same as the current calendar year. But it may not be around New Year's Day. The new week year does not always start on 1 January.
The answer lies in the documentation for IsoChronology
u
will give you the proleptic year. y
will give you the year of the era.
The difference is mainly important for years of the BC era. The proleptic year 0 is actually 1 BC, it is followed by proleptic year 1 which is 1 AD. The proleptic year can be negative, the year of era can not.
Here is a snippet that will help visualize how it works :
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("'proleptic' : u '= era:' y G"); for (int i = 5; i > -6 ; i--) { LocalDate localDate = LocalDate.of(i, 3, 14); System.out.println(formatter.format(localDate)); }
Output:
proleptic : 5 = era: 5 AD proleptic : 4 = era: 4 AD proleptic : 3 = era: 3 AD proleptic : 2 = era: 2 AD proleptic : 1 = era: 1 AD proleptic : 0 = era: 1 BC proleptic : -1 = era: 2 BC proleptic : -2 = era: 3 BC proleptic : -3 = era: 4 BC proleptic : -4 = era: 5 BC proleptic : -5 = era: 6 BC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With