Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat ParseException: Unparseable date Error

I'm parsing this date format from XML:=> "2011-12-06T07:41:14.016+00:00", and I'm getting this error:

  • W/System.err(574): java.text.ParseException: Unparseable date: "2011-12-06T07:41:14.016+00:00"

I'm certain it's the formatting statement I'm using, but I can't figure out what it SHOULD be...

Here's the statement I'm using:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ:ss");

I get how to create a format for this part: "2011-12-06T07:41:14....", it's this other part :=> ".016+00:00" that's throwing me for a loop.

I've looked for answers here already: Android SimpleDateFormat Page, and here Oracle SimpleDateFormat Page, but I fear I'm missing something fundamental....

Do you have any suggestions on a proper format statement for that particular date format, or pertinent resources to peruse?

Big Thanks in advance!

like image 875
ProfVersaggi Avatar asked Dec 06 '11 23:12

ProfVersaggi


People also ask

What is unparseable date error in Java?

The error java.text.ParseException: Unparseable date usually occurs while using the SimpleDateFormat class in Java. This class is used to format the date in Java. Most of the time, the error java.text.ParseException: Unparseable date occurs when we try to convert the string date into another desired date format.

Can simpledateformat parse the date with a colon in it?

I believe that SimpleDateFormat will not parse timezones with a colon in them (-08:00). It should be able to parse the date 2011-10-06T12:00:00-0800. Some simple string manipulation should help you get rid of the colon. Show activity on this post. You first need to format the value in "2011-10-06T12: 00: 00-08: 00".

What is the correct timezone for simpledateformat?

The timezone should be GMT-08:00 or -0800 (as Madcore Tom said). See Java docs. Show activity on this post. I believe that SimpleDateFormat will not parse timezones with a colon in them (-08:00).

How to get the date when formatDate does not have a date?

Now, in your formatDate () method, simple try all your known format and trap the Exception, then if still did not have a date, just use Date d = javax.xml.bind.DatatypeConverter.parseDateTime ("2013-06-28T00:00:00+00:00").getTime (); if (d == null) try { SimpleDateFormater ...


1 Answers

The "Z" pattern matches +0000 and not +00:00 so if you remove the last ":" before you parse then it will work.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
try {
  Date myDate = sdf.parse( "2011-12-06T07:41:14.016+00:00".replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
  System.out.println( myDate );
} catch (ParseException e) {
  e.printStackTrace();
}
like image 190
Neil Essy Avatar answered Oct 06 '22 02:10

Neil Essy