开发者

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

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 prop开发者_C百科osed 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?


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.


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".


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))


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜