简体   繁体   English

不可解析的日期java.text.ParseException:不可解析的日期:“ 2015-10-08 05:00:00.0”

[英]Unparsable Date java.text.ParseException: Unparseable date: “2015-10-08 05:00:00.0”

I have the following operation which should parse the date in the specified format. 我有以下操作,应以指定的格式解析日期。 But it does not parse and throws an exception of unparseable. 但是它不会解析,并引发不可解析的异常。 Can someone please correct me where i am wrong. 有人可以纠正我在哪里我错了。 The field is TimeStamp and DB is Oracle. 字段是TimeStamp,数据库是Oracle。

private XMLGregorianCalendar stringToXMLGregorianCalendar(String s)
    throws DatatypeConfigurationException, ParseException {
XMLGregorianCalendar result = null;
Date date = null;
SimpleDateFormat simpleDateFormat;
GregorianCalendar gregorianCalendar;

simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
    date = simpleDateFormat.parse(s);
    gregorianCalendar = (GregorianCalendar) GregorianCalendar
            .getInstance();
    gregorianCalendar.setTime(date);
    result = DatatypeFactory.newInstance().newXMLGregorianCalendar(
            gregorianCalendar);

} catch (java.text.ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

According to the JavaDoc , "yyyy-MM-dd'T'HH:mm:ss.SSSZ" would equate to something like 2001-07-04T12:08:56.235-0700 . 根据JavaDoc"yyyy-MM-dd'T'HH:mm:ss.SSSZ"将类似于2001-07-04T12:08:56.235-0700 If you notice, the T is still there. 如果您注意到, T仍然在那里。

In your error, the date seems to be the following: 2015-10-08 05:00:00.0” which is of the form yyyy-MM-dd HH:mm:ss.S , but you are using this format: yyyy-MM-dd'T'HH:mm:ss . 在您的错误中,日期似乎如下: 2015-10-08 05:00:00.0” ,其格式为yyyy-MM-dd HH:mm:ss.S ,但是您使用的是以下格式: yyyy-MM-dd'T'HH:mm:ss You need to omit the extra 'T' and include an extra .S to match the milli secon aspect of it. 您需要省略多余的'T'并包括多余的.S以匹配其毫微秒的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM