Neeraj
Neeraj

Reputation: 592

TimeZone ID coming incorrectly daylight time

Can anyone please explain this?

TimeZone tz1 = TimeZone.getTimeZone(“CST”); ==> ID of tz1 is CST
TimeZone tz2 = TimeZone.getTimeZone(“CDT”); ==> ID of tz2 is GMT ????

Upvotes: 3

Views: 937

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338564

tl;dr

CST & CDT are not time zones.

Use a real time zone such as America/Chicago.

ZoneId
.of( "America/Chicago" ) 
.getRules()
.isDaylightSavings( 
    Instant.now()
)

false

Pseudo-time-zones

TimeZone.getTimeZone(“CST”)

Whoops, that line makes no sense. CST is not a time zone.

Never use the 2-4 letter pseudo-time-zones such as PST, PDT, CST, IST, and so on. These are not real time zones. These are not standardized. These are not even unique! For example, I surmise you think of CST as “Central Standard Time”. But CST is also “China Standard Time”.

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;  

ZoneId

Dig to discover the real time zone used by the region in which you are interested. If by CST you meant one of the time zones in the midwest of North America, perhaps you really mean something like America/Chicago or America/Winnipeg.

ZoneId z = ZoneId.of( "America/Chicago" ) ;  

The America/Chicago time zone participates in the silliness known as Daylight Saving Time (DST). So we can ask if DST is currently engaged or not. This status of DST being currently in use or not is what is meant when the media publishes CST or CDT.

ZoneRules

Get the rules for that time zone.

ZoneRules rules = z.getRules() ;

Instant

Interrogate the status of DST and the offset at a specific moment. We will use the current moment here.

Instant now = Instant.now() ;

now.toString(): 2020-02-10T00:06:26.000116Z

ZonedDateTime

Look at that same moment as seen in our time zone.

ZonedDateTime zdt = now.atZone( z ) ;

zdt.toString(): 2020-02-09T18:06:26.000116-06:00[America/Chicago]

Now ask about offset and DST.

ZoneOffset offset = rules.getOffset( now );
boolean inDST = rules.isDaylightSavings( now );

offset.toString(): -06:00

inDST: false

See this code run live at IdeOne.com.

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

Upvotes: 0

Meno Hochschild
Meno Hochschild

Reputation: 44061

For me following test code produces "Not found => CDT":

for (String tz : TimeZone.getAvailableIDs()) {
    if (tz.equals("CDT")) {
        System.out.println("Found: " + tz);
    }
}

System.out.println("Not found => CDT");

That means java.util.TimeZone will fall back from non-existing CDT zone to "GMT" as documented:

Returns: the specified TimeZone, or the GMT zone if the given ID cannot be understood.

In Java 8 you have the alternative of ZoneId which has not the unintuitive fallback behaviour but instead throwing a ZoneRulesException which I consider to be much better.

Upvotes: 1

Related Questions