-->

Java daylight saving time does not work for distan

2019-07-06 14:41发布

问题:

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


标签: java date dst