Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TimeZone.setTimeZone("est") different from TimeZone.setTimeZone("EST")

Tags:

java

calendar

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

like image 979
Abubakkar Avatar asked Jan 27 '26 09:01

Abubakkar


1 Answers

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.

like image 81
Mathieu Le Tiec Avatar answered Jan 29 '26 23:01

Mathieu Le Tiec