Java daylight saving time does not work for distan

2019-07-06 14:11发布

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?

标签: java date dst
4条回答
地球回转人心会变
2楼-- · 2019-07-06 14:25

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楼-- · 2019-07-06 14:36

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
查看更多
做自己的国王
4楼-- · 2019-07-06 14:40

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-07-06 14:44

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

查看更多
登录 后发表回答