Reputation: 15664
When I write this code:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("EST"));
System.out.println(cal.getTimeZone().getDisplayName());
The output is
Eastern Standard Time
But when I write this code:
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("est"));
System.out.println(cal.getTimeZone().getDisplayName());
The output I get is:
GMT-05:00
Is there any difference in giving arguments like "EST" and "est" when setting TimeZone.setTimeZone(String str)
(Is str
to be passed when calling considered CASE SENSITIVE)?
The API doesn't mention anything about it:
getTimeZone
public static TimeZone getTimeZone(String ID)
Gets the TimeZone for the given ID.
Parameters:
ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00".
Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.
Returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.
NOTE: I tried with IST
and ist
strings. For IST
string it gives Indian Standard Time
and for ist
it gives Greenwich Mean Time
Upvotes: 3
Views: 16520
Reputation: 248
The implementation of getTimeZone(String id) actually changed from JDK 7 to 8.
In JDK 7 "est" is actually returning a timeZone with id "est". Running the following test case on java 7 will succeed (and fail on java 8):
@Test
public void estTimeZoneJava7() {
TimeZone timeZone = TimeZone.getTimeZone("est");
assertEquals("est", timeZone.getID()) ;
}
With Java 8 the timezone "est" is actually handled as a unknown timezone and will in fact return the GMT timezone with id "GMT". The following test case will succeed on Java 8 (and fail on java 7).
@Test
public void estTimeZoneJava8() {
TimeZone timeZone = TimeZone.getTimeZone("est");
assertEquals("GMT", timeZone.getID());
}
Upvotes: 3
Reputation: 501
In short, yes it is case sensitive.
Following your examples ist
gives you GMT
because a timezone with such an ID can't be find, thus giving you default result
it works with est
(GMT-05:00
is EASTERN STANDARD TIME
), surely because both IDs are known, but I wouldn't count on it (not too sure it would still be there if change platform).
Furthermore, as told by the API you shouldn't use those abbreviated IDs but directly full name or custom ID.
You can have a list of your platform's available IDs using TimeZone.getAvailableIDs()
, then you can pick the right one.
I would myself consider using GMT-5:00
format, which is in my opinion more readable and less error prone.
Upvotes: 3