Unparseable Date Error when parsing UTC string through SimpleDateFormat to Date

前提是你 提交于 2021-01-28 12:51:04

问题


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

Code:

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?


回答1:


Your input String needs to match your DateFormat pattern

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


来源:https://stackoverflow.com/questions/24791823/unparseable-date-error-when-parsing-utc-string-through-simpledateformat-to-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!