Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display UTC date/time into date/time according to the current time-zone

I am getting a date/time string from web in the format of "yyyy/mm/dd'T'HH:MM:SS'Z'" and it is in UTC.

Now i have to identify the current time zone of device and then convert this time to my local time..

How do i do it, pls suggest me !!

(FYI, Currently, UTC time is 10:25 AM, in india current time is 3:55 PM)

like image 969
Paresh Mayani Avatar asked Nov 01 '10 10:11

Paresh Mayani


People also ask

How do you display date time in UTC?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

How do you convert UTC to TimeZone?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

What is UTC in date format?

UTC, or Universal Time Coordinated, is the most precise and commonly referred to time standard. Since the 1970s, this time standard has been globally used as the most precise time standard, instead of formerly used GMT standard, which has turned now into a regular time zone.


1 Answers

Try using TimeZone.getDefault() instead of TimeZone.getTimeZone("GMT")

From the docs:

... you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running.

EDIT: You can parse date using SimpleDateFormat (there is also the documentation on the format string there). In your case, you want to do (untested):

 // note that I modified the format string slightly
 SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
 // set the timezone to the original date string's timezone
 fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
 Date date = fmt.parse("1998/12/21T13:29:31Z", new ParsePosition(0));

 // then reset to the target date string's (local) timezone
 fmt.setTimeZone(TimeZone.getDefault());
 String localTime = fmt.format(date);

alternatively, use two separate instances of SimpleDateFormat, one for original and one for target time.

like image 129
Lie Ryan Avatar answered Sep 30 '22 16:09

Lie Ryan