Sai's Stack
Sai's Stack

Reputation: 1385

java.lang.IllegalArgumentException: Parse error: Unable to parse Date format

I need to format following date fomat

 timeBooked: "2015-05-20T02:08:00.000Z",
 ExpiryTime: "2015-05-20T04:08:00.000Z",

My code follows to format the date:

try {
     Date currentDate = new Date(timeBooked);
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.ZZ", Locale.ENGLISH);
     Log.e(TAG, "formatted time string: " + sdf.format(currentDate.getTime()));

     Log.e(TAG, "date string:=" + currentDate.getDate());
} catch (Exception e) {
         Log.e(TAG, e.getMessage());
         e.printStackTrace();
}

While running this code getting java.lang.IllegalArgumentException: Parse error:2015-05-20T02:08:00.000Z.

Upvotes: 1

Views: 3697

Answers (3)

George Mulligan
George Mulligan

Reputation: 11903

Your format String is incorrect. It should be "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'".

Then to get your date correctly you should use:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
// Set time zone to UTC since 'Z' at end of String specifies it.
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 
// The parsed date will be offset from UTC in device's current TimeZone.
Date currentDate = sdf.parse(timeBooked);

Upvotes: 1

Basil Bourque
Basil Bourque

Reputation: 338181

Z Has Meaning

Both of the previous Answers tell you to expect-and-ignore the Z character, by surrounding with single quotes in the coded parsing pattern. Bad advice. You would be ignoring valuable data, and would be rejecting valid alternative inputs such as 2015-05-20T02:08:00.000+05:30. The pattern code Z means "any valid offset-from-UTC". Adding the single quotes for 'Z' says "expect an uppercase Z to appear here, ignore any meaning it may have, and throw an exception if the Z is missing".

Joda-Time

You are using the old date-time classes bundled with early versions of Java. Those classes have proven to be troublesome, flawed in both design and implementation. Avoid them. In current Android, add the Joda-Time library to your project. In Java 8 and later, use the java.time framework that was inspired by Joda-Time.

Your string inputs are in ISO 8601 standard format. Both Joda-Time and java.time use ISO 8601 as their defaults when parsing/generating textual representations of date-time values. So you these classes can directly parse such strings without you needing to specify any coded parsing patterns.

The following code creates a date-time assigned an offset-from-UTC of 0, which is what the Z (for Zulu) means.

DateTime dateTime = DateTime.parse( "2015-05-20T02:08:00.000Z" );

Using a constructor has a different meaning. The following code parses the value with an offset of zero but then adjusts the results into your JVM’s current default time zone.

DateTime dateTime = new DateTime( "2015-05-20T02:08:00.000Z" ) );

I suggest you always explicitly assign your desired/expected time zone.

DateTimeZone zone = DateTimezone.forID( "Europe/Paris" );
DateTime dateTime_Europe_Paris = dateTime.withZone( zone );

If you really need a java.util.Date, convert after doing your parsing and business logic with Joda-Time.

java.util.Date date = dateTime.toDate();

Search StackOverflow for many more Questions and Answers with example code for Joda-Time.

Upvotes: 0

Stephen Rosenthal
Stephen Rosenthal

Reputation: 773

The Date constructor taking a string is deprecated: see http://developer.android.com/reference/java/util/Date.html#Date(java.lang.String). It won't accept a custom date format; only a pre-defined set of formats are allowed.

Once you get your DateFormat string correct (I think "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" should work) you can call sdf.parse(timeBooked) to get a valid Date.

Upvotes: 0

Related Questions