I get inconsistent results when converting Date to LocalDate s for about 200 years. Using the following code to convert:
private LocalDate toLocalDate(Date localDate) { return LocalDateTime.ofInstant(localDate.toInstant(), ZoneId.systemDefault()).toLocalDate(); }
My ZoneId.systemDefault() is Africa/Harare , which corresponds to the CAT used in the test. The run I run
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); String dateString = "Tue Jan 01 00:00:00 CAT 200"; String dateString2 = "Tue Jan 01 00:00:00 CAT 201"; String dateString3 = "Wed Dec 31 00:00:00 CAT 200"; System.out.println(toLocalDate(simpleDateFormat.parse(dateString))); System.out.println(toLocalDate(simpleDateFormat.parse(dateString2))); System.out.println(toLocalDate(simpleDateFormat.parse(dateString3)));
My expected result for this would be
0200-01-01 0201-01-01 0200-12-31
Or, if not this, at least the invariably incorrect values. Actual Results:
0199-12-31 0201-01-01 0200-12-31
So, it seems that the first one is a bit of a rollback, perhaps two hours, corresponding to the CAT time zone? But why does this happen in only one case? Performing the same experiment since 2000 does not lead to the same error.
java date java-8 java-time
Evan knowles
source share