I just want to share my experience about Java TimeZone. Here was the problem:
The inDaylightTime(Date date) function of timezone always returns 0, regardless of date. Consistently getDSTSavings() also returns 0. here is the snippet of code to create timezone:

Timezone timezone = TimeZone.getTimeZone("GMT+1:00");
有帮助吗?

解决方案

DST in a timezone object created with an id like "UTC+1:00" (or "GMT+1:00") will be different with a timezone object created with corresponding string "Europe/Berlin", so if DST is important to your application, always use full string id's instead of corresponding time offset.
So changing timezone definition to:

Timezone timezone = TimeZone.getTimeZone("Europe/Berlin");

will solve the problem.

其他提示

No, TimeZone API figures out day light savings. You are using a custom time zone ID.

From the Documentation of TimeZone API

No daylight saving time transition schedule can be specified with a custom time zone ID

So, you need to specify the time zone ID available to get day light savings

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.You can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a U.S. Pacific Time TimeZone object with:

Use the time zone ID, this will take care of day light savings in that particular zone

 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

tl;dr

Ask if DST is currently in effect.

ZoneId
.of( "Europe/Madrid" )
.getRules()
.isDaylightSavings​( Instant.now() )

Ask the offset (hours-minutes-seconds) ahead or behind UTC currently in effect.

ZoneId
.of( "Africa/Tunis" )
.getRules()
.getOffset​( Instant.now() )

DST comes and goes

To ask "Is Daylight Saving Time in effect?", you must specify a moment. The very definition of Daylight Saving Time (DST) is that it comes and goes, twice a year.

Use Instant to specify a moment.

Instant instant = Instant.now() ;  // Capture the current moment as seen in UTC.

java.time

You are using terrible date-time classes that are now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.

Table of all date-time types in Java, both modern and legacy

Use ZoneId rather than TimeZone.

Offset versus time zone

TimeZone.getTimeZone("GMT+1:00");

The string GMT+1:00 does not represent a time zone, it represents an offset. There are many time zones that may all coincidentally be using an offset right now of one hour ahead of UTC, such as Africa/Casablanca, Africa/Brazzaville, Africa/Tunis, Europe/Andorra, Europe/Warsaw, and many more.

Understand that an offset is merely a number of hours-minutes-seconds ahead or behind the prime meridian. An offset looks like +05:30 or -05:00.

A time zone is much more. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region. The rules of a time zone are set capriciously by politicians, and change with surprising frequency.

A proper time zone name is composed as Continent/Region such as America/Montreal or America/New_York. See this list of zones at Wikipedia (may not be up-to-date).

ZoneId z = ZoneId.of( "Europe/Gibraltar" ) ;

Asking "Is DST in effect?"

It seems you want to know if DST is currently in effect for a particular time zone. Get the ZoneRules for a particular ZoneId. Then interrogate for a specific moment.

ZoneId z = ZoneId.of( "Europe/Gibraltar" ) ;
ZoneRules rules = z.getRules() ;
boolean dstInEffect = rules.isDaylightSavings​( Instant.now() ) ;

And you can ask for the amount of the offset-from-UTC currently in effect. A ZoneOffset object represents that number of hours-minutes-seconds ahead or behind the prime meridian.

ZoneOffset offset = rules.getOffset​( Instant.now() ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top