MadTech MadTech - 4 months ago 10
Android Question

UTC time into Local TIme in Android

In my project, i have get the api response in Json format. I get the string value of time in UTC time format of that is

Jul 16, 2013 12:08:59 AM
. I need to change this into Local time.
That is where ever we use this app need to show our local time. How to I do?

Here 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 contain
Jul 16, 2013 12:08:59 AM

Answer

Here's my attempt:

String dateStr = "Jul 16, 2013 12:08:59 AM";
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
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...