Android将UTC日期转换为本地时区 [英] Android convert UTC Date to local timezone

查看:589
本文介绍了Android将UTC日期转换为本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的API获取此日期字符串:2015-12-07T14:11:15.596Z

I get this date string from my API : "2015-12-07T14:11:15.596Z"

但这个日期是UTC格式,我想在当地时间转换它,我该怎么做?

But this date is in UTC format and I want to convert it in local time, how can I do it ?

我试过这个:

try
{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    return simpleDateFormat.parse(this.created_at);
}
catch (ParseException e)
{
    Log.e("Error Date at Whisp", e.getMessage());
    return null;
}

但它返回给我这个错误:

But it return me this error :

Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 10)


推荐答案

您的日期格式模式错误。更改为:

your Date Format pattern is wrong. Change to:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");

有关更多信息,请参阅 SimpleDateFormat

For more informations see the javadoc of SimpleDateFormat

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

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