1

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.

2
  • 1
    Isn't your format String really "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" Commented Jan 19, 2016 at 2:44
  • I will try this...And let you know Commented Jan 19, 2016 at 2:46

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This Answer is incorrect. Putting the single-quote marks around the Z means "expect to see this character, ignore it, but tell me if it is not present". You would be ignoring crucial information, the offset-from-UTC. If the value is indeed a "Z", it means "Zulu" which means an offset of 0, at UTC. But other valid offset values such as -07:00 will throw an exception as if the input were invalid when in fact it would be valid.
@BasilBourque Good catch. I forgot to set the TimeZone on the formatter. I updated the answer.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.