Вы находитесь на странице: 1из 5

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

Converting UTC dates to other timezones


20

android

datetime

timezone

I'm converting a UTC time to another timezone, using this method:


SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
format.setTimeZone(tz);
String result = format.format(parsed);

So the input is 2011-03-01 15:10:37 but the output of this (value of result) is 2011-03-01 05:40:37. While it seems off, and according to this link, it
should be 2011-03-01 09:10:37.
What am I doing wrong?

share

improve this question


Hadi Eskandari
8,829 6 38
Sufian
2,654

3 Answers

58

5 21 49

Asked
May 22 '11 at 15:07

Edited
Jul 8 '14 at 9:00

order by votes

43

It turns out the code was almost correct, what I didn't take into account was that when parsing the String to get a Date object initially, it
uses default system TimeZone, so the source date was not in UTC as I expected.
The trick was to set the timezone when parsing the date to UTC and then set it to destination TimeZone. Something like this:
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = format.parse("2011-03-01 15:10:37"); // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);

share

improve this answer


Hadi Eskandari
8,829 6 38
Sufian
2,654

58

5 21 49

Answered
May 23 '11 at 8:07

Edited
Jul 8 '14 at 8:57

don't forget the try{} catch (ParseException e){} around sourceFormat.parse(). Also, to get the local timezone that the phone is in then use TimeZone.getDef
ault() so that you can convert UTC to local timezone Someone Somewhere Feb 13 '13 at 0:32
@hadi Eskandari Mate you just saved me! I was tearing my hair out trying to figure this problem...! I pull down values from our server and store to a
Database before i display to screen. The server dates are stored in AEST (Australian Eastern Standard) and I was having your exact problem where it was
using the default system timezone so interpreting the time wrong. When i tried changing to another date it was always wrong :) I just needed formater.setTi
meZone(TimeZone.getTimeZone(RBApplication.adjustTimezoneAEST)); before my parsing line. Cheers i was on this problem for a few hours! wired00
Feb 26 '13 at 7:43
@wired00 Happy to hear that helps mate :) Hadi Eskandari Feb 26 '13 at 10:16
the "HH" in SimpleDateFormat is for 24 hours and not for 12 hours, that ate up my 2 hours of debugging khurramengr Jan 15 at 10:52

add a comment

Converting a date String of the format "2011-06-23T15:11:32" to out time zone.


private String getDate(String dateString) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = null;
try {
value = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mmaa");
dateFormatter.setTimeZone(TimeZone.getDefault());
String dt = dateFormatter.format(value);
}

share

return dt;

improve this answer


QBLive
143 2

Jon Adams
13.5k 10

Answered
Jun 6 '11 at 10:49

10

47 78

Edited
Oct 18 '12 at 20:56

You need to take Daylight Savings into consideration. I do this by working out the offset (from UTC) in millieseconds. Something like
this should work.
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(parsed) ? tz.getDSTSavings() : 0);
String result = format.format(parsed.getTime() + currentOffsetFromUTC);

The inDayLightTime(...) method returns a boolean and must be passed a Date object in order for it to decide if that 'date' represents one
during a DST period or not.

share

improve this answer


Squonk
35.7k

16 72 111

Your Answer

log in
or
Name

Email

Answered
May 22 '11 at 17:08

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

Вам также может понравиться