0

For my work, I wanted to get an Instant date for a given date as per given ZoneID, Everything was wroking fine, but when I checked with old date (year 1883 or before). The time appears as 04:56, instead of 5:00.

For any date after 18 nov 1883, it gives me correct result like 1883-11-19T05:00:00Z

Can someone helps, why time appear as 4:56 for older dates ?

public static String firstdate = "18831118";

LocalDate localFirstDate = LocalDate.parse(firstdate, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDateTime localDateTime = localFirstDate.atStartOfDay();
Instant instantFirstDate = localDateTime.toInstant(ZoneId.of("America/New_York").getRules().getOffset(localDateTime));

Output

1883-11-18T04:56:02Z
public static String firstdate = "18831119";

LocalDate localFirstDate = LocalDate.parse(firstdate, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDateTime localDateTime = localFirstDate.atStartOfDay();
Instant instantFirstDate = localDateTime.toInstant(ZoneId.of("America/New_York").getRules().getOffset(localDateTime));

Output

1883-11-19T05:00:00Z

In the later case, it shows time perfectly 5:00:00Z

gaurav
  • 51
  • 1
  • 6

1 Answers1

1

(Assuming localDateTime is declared as LocalDateTime localDateTime = localFirstDate.atStartOfDay();)

If you look at what's returned by ZoneId.of("America/New_York").getRules().getOffset(localDateTime), you'll see that it is an offset of about -4:56. The instants are calculated using this offset, so they have the time of 04:56.

Why does New York has such a weird time zone offset in 1883 before November 17? Apparently, on November 18, Standard time zones forms by railroads in US & Canada. It is easy to imagine that there is no standard on-the-hour time zones before that date.

If you look at other time zones, such as Asia/Macau, you'll see that most of them have weird offsets more than a hundred years ago. Messing up time zones is a natural thing that we humans do :).

In your specific case, if you would like to use the standardised time zone offset before 18 November 1883, you could just apply the ZoneOffsetTransitionRule you get from ZoneId.of("America/New_York").getRules().getTransitionRules() manually, but I am not sure how much sense that would make.

Sweeper
  • 213,210
  • 22
  • 193
  • 313