开发者

Date format error with "2011-07-27T06:41:11+00:00"

I'm trying to format a time/date string:

String date = "2011-07-27T06:41:11+00:00";
DateFormat formatter = new SimpleDateFormat("yyyy MM-dd'T'HH:mm:ssz"); //2011-07-27T06:41:11+00:00
Date Sdate = formatter.parse(date.toString());

This is throwing the error

unable to parse newDate.

I don't understand开发者_JAVA百科 why I'm getting this error, can someone explain?


The issue is with the TimeZone information. The ':' is an illegal character in the timezone string. See http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone and http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#timezone

The following string is parseable

String date = "2011-07-27T06:41:11+0000";

Remove the ':' and your code will work.


SimpleDateFormat do not accept all ISO8601 date-time formats .

You can use DatatypeConverter.parseDateTime in JAXB .

something like

String date = "2011-07-27T06:41:11+00:00";
Date Sdate = DatatypeConverter.parseDateTime(date).getTime();

and please do try to follow the conventions (variable names should start with a lower case)


Try JodaTime. Java's built-in Date handling is not that good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜