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
Use the following code. TimeZone defaultTimeZone = TimeZone. getDefault(); String strDefaultTimeZone = defaultTimeZone. getDisplayName(false, TimeZone.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With