通过SimpleDateFormat将UTC字符串解析为Date时发生不可解析的日期错误 [英] Unparseable Date Error when parsing UTC string through SimpleDateFormat to Date

查看:77
本文介绍了通过SimpleDateFormat将UTC字符串解析为Date时发生不可解析的日期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些采用UTC时间的以下格式的字符串,并将其格式化为从手机获取的本地时区.但是,尽管查看了 SimpleDateFormat文档,但我还是遇到了该错误我的示例字符串不可解析:

I'm trying to write something that will take in a string with the below format in UTC time and format it to the local time zone grabbed from the phone. However, despite looking at SimpleDateFormat documentation, I'm running across the error that my sample string is unparseable:

W/System.err﹕ java.text.ParseException: Unparseable date: "2014-07-16T21:00:00:000+0000" (at offset 19)
W/System.err﹕ at java.text.DateFormat.parse(DateFormat.java:555)
[...]
D/Set time:﹕ Wed Jul 16 15:10:03 PDT 2014
D/TimeInMilli:﹕ 1405548603565

代码:

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone(); //Get phone's PST zone

String UTCinput = "2014-07-16T21:00:00:000+0000"; //2:00PM PST
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(tz);

Date dateCal = new Date();

try {
    dateCal = format.parse(UTCinput);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

cal.setTime(dateCal);
Log.d("Set time: ", String.valueOf(dateCal));
Log.d("TimeInMilli: ", String.valueOf(cal.getTimeInMillis()));

我的解析方式有什么问题?

What is the issue with the way I'm parsing?

推荐答案

您的输入 String 需要与您的 DateFormat 模式匹配

Your input String needs to match your DateFormat pattern

String UTCinput = "2014-07-16T21:00:00.000+0000"; //2:00PM PST
                                      ^--- was ":"

这篇关于通过SimpleDateFormat将UTC字符串解析为Date时发生不可解析的日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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