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?
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?
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