Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC into Local Time on Android

Tags:

java

android

utc

In my project, I have get the API response in json format. I get a string value of time in UTC time format like this Jul 16, 2013 12:08:59 AM.
I need to change this into Local time. That is where ever we use this the app needs to show the local time. How to I do this?

Here is some Code I have tried:

String aDate = getValue("dateTime", aEventJson); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z"); simpleDateFormat.setTimeZone(TimeZone.getDefault()); String formattedDate = simpleDateFormat.format(aDate); 

Assume aDate contains Jul 16, 2013 12:08:59 AM

like image 510
MadTech Avatar asked Jul 16 '13 13:07

MadTech


People also ask

How do I convert UTC timestamp to device local time in Android?

Use the following code. TimeZone defaultTimeZone = TimeZone. getDefault(); String strDefaultTimeZone = defaultTimeZone. getDisplayName(false, TimeZone.

How do you format UTC time?

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 I get time zone on my Android?

According to http://developer.android.com/reference/android/text/format/Time.html you should be using Time. getCurrentTimezone() to retrieve the current timezone of the device.


1 Answers

Here's my attempt:

String dateStr = "Jul 16, 2013 12:08:59 AM"; SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH); df.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = df.parse(dateStr); df.setTimeZone(TimeZone.getDefault()); String formattedDate = df.format(date); 

Also notice the "a" for the am/pm marker...

like image 156
devconsole Avatar answered Sep 19 '22 19:09

devconsole