Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC to current locale time

Tags:

java

android

utc

I am downloading some JSON data from a webservice. In this JSON I've got some Date/Time values. Everything in UTC. How can I parse this date string so the result Date object is in the current locale?

For example: the Server returned "2011-05-18 16:35:01" and my device should now display "2011-05-18 18:35:01" (GMT +2)

My current code:

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime")); 
like image 654
LongFlick Avatar asked May 18 '11 18:05

LongFlick


People also ask

How do you convert UTC to local time zone?

(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.

How do I convert UTC to local time in Excel?

(2) The formula =A2 + (9 / 24) will return a decimal number. For converting the decimal number to time, please select the decimal number, and click Home > Number Format > Time.

How do I convert UTC time to local time in SQL?

SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.


2 Answers

It has a set timezone method:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime")); 

all done!

like image 168
Affe Avatar answered Oct 16 '22 14:10

Affe


So you want to inform SimpleDateFormat of UTC time zone:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); TimeZone utcZone = TimeZone.getTimeZone("UTC"); simpleDateFormat.setTimeZone(utcZone); Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime")); 

To display:

simpleDateFormat.setTimeZone(TimeZone.getDefault()); String formattedDate = simpleDateFormat.format(myDate); 
like image 36
Paweł Dyda Avatar answered Oct 16 '22 12:10

Paweł Dyda