8

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?

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

1 Answer 1

14

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.

4
  • @Lei Ryan but dear i know how to do parsing and formatting the date, but want to display UTC into Local timezone Nov 1, 2010 at 11:07
  • @Paresh Mayani: you should say so in your question. See my updated answer
    – Lie Ryan
    Nov 1, 2010 at 11:39
  • @Lei Ryan This is working!! Found your past answer very helpful, from that i done the same code as you have done. Btw, thanx for the quick response and support Nov 1, 2010 at 12:06
  • Are you sure that's the format you've got? mm is minutes, MM is months. It would look very odd to have minutes in between years and days
    – NickT
    Nov 1, 2010 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.