Problem to represent midnight for 01 May 1921 by java.util.Date class
I want to represent midnight by java.util.Date class. To check this I output this value and obtain unexpected result.
The code foll开发者_StackOverflowows:
Calendar calendar = new GregorianCalendar(1921, 4, 1, 0, 0);
Date date2 = calendar.getTime();
System.out.println(date2);
I obtain the following output:
Sun May 01 00:20:08 EET 1921
But I expect to obtain
Sun May 01 00:00:00 EET 1921
What is the cause of such behavior?
Currently I am testing my code in Europe/Helsinki timezone.
Why don't you simply check the time zone history for your country? You asked a similar question just a few days ago. Finland had a very odd time zone GMT+1:39:52 until April 30th, 1921. From May 1st, they switched to GMT+2:00 and jumped from 23:59:59 to 00:20:08.
Luckily I still had my test code from last time handy ;-)
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Europe/Helsinki"));
calendar.set(1921,4, 1, 0, 0);
Date date2 = calendar.getTime();
System.out.println("GregorianCalendar: " + date2);
DateTime c2 = new DateTime(DateTimeZone.forID("Europe/Helsinki")).withYear(1921).
withMonthOfYear(5).withDayOfMonth(1).withHourOfDay(0). withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
System.out.println("Joda: " + c2);
}
gives
GregorianCalendar: Sat Apr 30 23:20:43 CEST 1921
Exception in thread "main" org.joda.time.IllegalFieldValueException: Value 0 for minuteOfHour is not supported: Illegal instant due to time zone offset transition: 1921-05-01T00:00:35.781 (Europe/Helsinki)
at org.joda.time.chrono.ZonedChronology$ZonedDateTimeField.set(ZonedChronology.java:469)
at org.joda.time.DateTime.withMinuteOfHour(DateTime.java:1485)
Actually lots of time-zones give strange result before Oct 1979. I suggest you look at Joda-time if you want accurate historical time zones.
The worst day appears to be 1 Jan 1900 for some reason. Try
Calendar calendar = new GregorianCalendar(1900, 1, 1, 0, 0);
Date date2 = calendar.getTime();
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println("Time in UTC is " + date2);
for (String tz : TimeZone.getAvailableIDs()) {
TimeZone.setDefault(TimeZone.getTimeZone(tz));
String dateToString = date2.toString();
if (dateToString.contains(":00:00")) continue;
if (dateToString.contains(":30:00")) continue;
System.out.println(tz + ' ' + dateToString);
}
Before 1900, very few timezones produce these sort of results (perhaps these are the inaccurate ones)
Upvote to jarnbjo above, would Joda-Time help any of your problems?
The Java Date/Time/Calendar libraries are a bit broken at times.
精彩评论