What is the difference between year and year-of-era?

Java 8Java Time

Java 8 Problem Overview


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?

Java 8 Solutions


Solution 1 - Java 8

The answer lies in the documentation for IsoChronology

  • era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE).
  • year-of-era - The year-of-era is the same as the proleptic-year for the current CE era. For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards.
  • proleptic-year - The proleptic year is the same as the year-of-era for the current era. For the previous era, years have zero, then negative values.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionglerupView Question on Stackoverflow
Solution 1 - Java 8bowmoreView Answer on Stackoverflow