在Android上将UTC转换为本地时间 [英] Convert UTC into Local Time on Android

查看:157
本文介绍了在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 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);

假设aDate包含 2013年7月16日上午12:08:59

推荐答案

这是我的尝试:

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);

另请注意am / pm标记的a...

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

这篇关于在Android上将UTC转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆