Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change TIMEZONE for a java.util.Calendar/Date

I would like to change the TIMEZONE value in a Java Calendar instance at runtime. I tried below. But the output is the same in both instances:

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));     System.out.println(cSchedStartCal.getTime().getTime());     cSchedStartCal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));     System.out.println(cSchedStartCal.getTime().getTime()); 

OUTPUT:
1353402486773
1353402486773

I have tried this also but the output is still the same:

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));     System.out.println(cSchedStartCal.getTime());      Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));     cSchedStartCal1.setTime(cSchedStartCal.getTime());     System.out.println(cSchedStartCal.getTime()); 

In the API I am seeing the below comment but I am not able to understand much of it:

     * calls: cal.setTimeZone(EST); cal.set(HOUR, 1); cal.setTimeZone(PST).      * Is cal set to 1 o'clock EST or 1 o'clock PST?  Answer: PST.  More      * generally, a call to setTimeZone() affects calls to set() BEFORE AND      * AFTER it up to the next call to complete(). 

Could you please help me?

One Possible Solution :

    Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));     long gmtTime = cSchedStartCal.getTime().getTime();      long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone("Asia/Calcutta").getRawOffset();     Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));     cSchedStartCal1.setTimeInMillis(timezoneAlteredTime); 

Is this solution ok?

like image 346
Kanagavelu Sugumar Avatar asked Nov 20 '12 10:11

Kanagavelu Sugumar


People also ask

How do I change the TimeZone in Java Util date?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

Does Java Util date have TimeZone?

The java. util. Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print the Date object directly, the Date object will be always printed with the default system time zone.

How do I convert a date time field for any TimeZone?

Calendar calendar = new GregorianCalendar(TimeZone. getTimeZone("GMT")); DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); formatter. setTimeZone(TimeZone.


1 Answers

In Java, Dates are internally represented in UTC milliseconds since the epoch (so timezones are not taken into account, that's why you get the same results, as getTime() gives you the mentioned milliseconds).
In your solution:

Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); long gmtTime = cSchedStartCal.getTime().getTime();  long timezoneAlteredTime = gmtTime + TimeZone.getTimeZone("Asia/Calcutta").getRawOffset(); Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta")); cSchedStartCal1.setTimeInMillis(timezoneAlteredTime); 

you just add the offset from GMT to the specified timezone ("Asia/Calcutta" in your example) in milliseconds, so this should work fine.

Another possible solution would be to utilise the static fields of the Calendar class:

//instantiates a calendar using the current time in the specified timezone Calendar cSchedStartCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); //change the timezone cSchedStartCal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); //get the current hour of the day in the new timezone cSchedStartCal.get(Calendar.HOUR_OF_DAY); 

Refer to stackoverflow.com/questions/7695859/ for a more in-depth explanation.

like image 130
KeyNone Avatar answered Sep 21 '22 12:09

KeyNone