Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert util.Date to time.LocalDate correctly for dates before 1893

Tags:

I googled for a while and the most commonly used method seems to be

date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

However, this method seems to fail for dates before 1893-04-01

The following test fails on my machine with an outcome of 1893-03-31 instead of 1893-04-01:

@Test
public void testBeforeApril1893() throws ParseException {
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1893-04-01");

    System.out.println(date);

    LocalDate localDate2 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

    System.out.println(localDate2);

    assertEquals(1893, localDate2.getYear());
    assertEquals(4, localDate2.getMonth().getValue());
    assertEquals(1, localDate2.getDayOfMonth());
}

The System.out.prinlns are for me to double check the created dates. I see the following output:

Sun Apr 02 00:00:00 CET 1893
1893-04-02
Sat Apr 01 00:00:00 CET 1893
1893-03-31

For 1400-04-01 I even get an output of 1400-04-09.

Is there any method to convert dates before 1893-04 correctly to LocalDate?

As some helpfully pointed out, the reason for this shift is explained in this question. However, I don't see how I can deduce a correct conversion based on this knowledge.