75

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?

glerup
  • 1,352
  • 1
  • 10
  • 11
  • http://stackoverflow.com/questions/26431882/difference-between-year-of-era-and-week-based-year – Don Larynx Mar 12 '15 at 15:48
  • 1
    @DonLarynx Yes. There is also a 3rd symbol Y for week-based-year. I know the motivation for that. But that does not answer my question about the difference between u and y. – glerup Mar 12 '15 at 15:52
  • There’s a later duplicate with a couple more ansers: [`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?](https://stackoverflow.com/questions/41177442/uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java) – Ole V.V. Mar 21 '20 at 06:19

1 Answers1

79

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
bowmore
  • 9,879
  • 1
  • 31
  • 38
  • 2
    Thanks. I found further discussions here: http://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoLocalDate.html http://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html#YEAR_OF_ERA – glerup Mar 12 '15 at 16:04
  • 8
    The other chronologies can have their own eras too, particularly [JapaneseChronology](https://docs.oracle.com/javase/8/docs/api/java/time/chrono/JapaneseChronology.html) – Jeffrey Bosboom Mar 12 '15 at 16:05