EPMS Devteam
EPMS Devteam

Reputation: 273

New Date API - Add Era

I'm playing with the new Date API and, just out of curiosity, I've tried to run the following code:

LocalDateTime timePoint = LocalDateTime.now(ZoneId.of("Australia/Canberra"));

System.err.println(timePoint.plus(1, ChronoUnit.ERAS));

And got the following error:

Exception in thread "main" java.time.DateTimeException: Invalid value for Era (valid values 0 - 1): 2
       at java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
       at java.time.temporal.ChronoField.checkValidValue(ChronoField.java:703)
       at java.time.LocalDate.with(LocalDate.java:1023)
       at java.time.LocalDate.plus(LocalDate.java:1245)
       at java.time.LocalDateTime.plus(LocalDateTime.java:1194)
       at pt.sibs.epms.Tester.method6(Tester.java:79)
       at pt.sibs.epms.Tester.main(Tester.java:59)

Is this a bug or I just am not using it correctly?

Upvotes: 0

Views: 204

Answers (1)

Jesper
Jesper

Reputation: 207006

The documentation of the plus method that you are calling says:

Throws: DateTimeException if the addition cannot be made

You are using the standard ISO chronology, which uses IsoEra. The ISO chronology has two eras:

  • BCE = before current era (before the year 0)
  • CE = current era (starting from the year 0)

If you have a date which is in the CE and you try to go to the next era, you get a DateTimeException because there is no next era, so the addition cannot be made and the exception is thrown, which is according to the documentation, so this is not a bug.

Upvotes: 1

Related Questions