UTC时间成安卓当地时间 [英] UTC time into Local TIme in Android
本文介绍了UTC时间成安卓当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目,我已经得到JSON格式的API响应。我得到的时间,那UTC时间格式的字符串值 2013年7月16日上午12时08分59秒
。我需要更改此为本地时间。
这就是以往我们使用这个程序需要展示我们的本地时间。怎么办?
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?
下面一些code我曾尝试:
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);
假设A日期包含 2013年7月16日上午十二点08分59秒
推荐答案
下面是我的尝试:
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);
另外,还要注意一为AM / PM标记...
Also notice the "a" for the am/pm marker...
这篇关于UTC时间成安卓当地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文