Java daylight saving time does not work for distant past (UPDATE: It does)?

拜拜、爱过 提交于 2019-12-14 01:45:39

问题


The following piece of code:

TimeZone.getTimeZone("Europe/Athens").inDaylightTime(new Date(200, 8, 14));

returns true, much like it does for the year 2011. However, Daylight Saving Time (DST) was only proposed 100 years or so ago, and applied even more recently. Is the time in the year 200 considered DST, or is this a Java quirk?


回答1:


You are mistaken. It works as expected when you use the date new Date(-1700, 8, 14) (which is year 200). The constructor you are using is adding 1900 to your year. You are actually using year 2100.

Check the Date constructor api.




回答2:


It depends on the timezone database. Some locales come with ridiculously detailed time instructions (such as recording 8-minute changes in UTC offsets back in the 1800s), whereas others are content with being useful for times reasonably close to the date they are created -- perhaps because the compiler of the database did not have easy access to information about when the current scheme was instituted and what came before it, and therefore coded it as "applies to all times before such-and-such".




回答3:


The date you are testing is

Sep 14, 2100 12:00 AM

Not in past, but in future! To print exact date, try with

DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.SHORT).format(new Date(200, 8, 14))




回答4:


For fun I tried this in Joda-Time 2.3 with Java 7. Looks like it works correctly.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime dateTime = new DateTime( 200, 8, 14, 0, 0, 0, DateTimeZone.forID( "Europe/Athens" ) );
boolean isDST = !( DateTimeZone.forID( "Europe/Athens" ).isStandardOffset( dateTime.getMillis() ) );

Dump to console…

System.out.println( "dateTime: " + dateTime );
System.out.println( "isDST: " + isDST );

When run…

dateTime: 0200-08-14T00:00:00.000+01:34:52
isDST: false


来源:https://stackoverflow.com/questions/7232750/java-daylight-saving-time-does-not-work-for-distant-past-update-it-does

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!