Java:日期解析,为什么我会收到错误 [英] Java: Date parsing, why do I get an error
本文介绍了Java:日期解析,为什么我会收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ");//2018-02-05T18:00:51.001+0000
String text = dateFormat.format(date);
Date test = dateFormat.parse(text);
前三行正常。当我尝试再次将字符串解析为日期时,我收到错误。我该如何解决这个问题?
The first three lines work fine. When I try to parse the string into a date again I get an error. How can I solve this?
错误如下所示:
Caused by: java.text.ParseException: Unparseable date: "2018-02-07T15:32:13.214+0100"
at java.text.DateFormat.parse(DateFormat.java:366) ~[na:1.8.0_151]
at TimeRange.parseDateFromIsoString(TimeRange.java:33) ~[classes/:na]
推荐答案
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");//2018-02-05T18:00:51.001+0000
String text = dateFormat.format(date);
try {
Date test = dateFormat.parse(text);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
为我工作。在模式的末尾使用SSSZ而不是SZ。
worked for me. With "SSSZ" instead of "SZ" at the end of the pattern.
这篇关于Java:日期解析,为什么我会收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文