Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UTC dates to other timezones

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?

like image 599
Hadi Eskandari Avatar asked May 22 '11 15:05

Hadi Eskandari


People also ask

How do you convert UTC to specific time zones?

The best way to do this is simply to use TimeZoneInfo. ConvertTimeFromUtc . // you said you had these already DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0); TimeZoneInfo tzi = TimeZoneInfo. FindSystemTimeZoneById("Pacific Standard Time"); // it's a simple one-liner DateTime pacific = TimeZoneInfo.

How do I convert a date to a different time zone?

To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.

How do I convert UTC to time zones in Excel?

=A1+Time(5,30,0) You'll need to add 5 hours and 30 minutes for IST. Similarly, by adding or removing the time difference, you can convert to any timezone. Hours, minutes, and seconds are the three inputs to Time(). This function returns a serial number formatted timestamp.

How do you convert date to UTC format?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.


2 Answers

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 = sourceFormat.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); 
like image 100
Hadi Eskandari Avatar answered Oct 05 '22 17:10

Hadi Eskandari


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);      return dt; } 
like image 40
QBLive Avatar answered Oct 05 '22 17:10

QBLive