5

I have the following code:

  String ModifiedDate = "1993-06-08T18:27:02.000Z" ;  
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
  Date ModDate = sdf.parse(ModifiedDate);

I am getting the following exception even though my date format is fine...

java.text.ParseException: Unparseable date: "1993-06-08T18:27:02.000Z"
at java.text.DateFormat.parse(DateFormat.java:337)
demongolem
  • 9,474
  • 36
  • 90
  • 105
user2133404
  • 1,816
  • 6
  • 34
  • 60
  • Try single quote the 'Z' SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); – gtgaxiola Jun 12 '14 at 19:27
  • Do you want to use ISODateTimeFormat (which is in joda)? Might be better for that ISO8601 standard. See http://stackoverflow.com/questions/5393847/how-can-i-convert-a-timestamp-from-yyyy-mm-ddthhmmsssssz-format-to-mm-dd-yyyy – demongolem Jun 12 '14 at 19:28

3 Answers3

10

The Z pattern latter indicates an RFC 822 time zone. Your string

String ModifiedDate = "1993-06-08T18:27:02.000Z" ;  

does not contain such a time zone. It contains a Z literally.

You'll want a date pattern, that similarly to the literal T, has a literal Z.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

If you meant for Z to indicate Zulu time, add that as a timezone when constructing the SimpleDateFormat

sdf.setTimeZone(TimeZone.getTimeZone("Zulu"));;
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I will add change the `ModifiedDate` to take a time zone difference such as '2001-07-04T12:08:56.235-0700' [SampleDateFormat Examples section](http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) – gtgaxiola Jun 12 '14 at 19:32
  • @gtgaxiola Absolutely, if OP can change their input string, it might be easier to do that. – Sotirios Delimanolis Jun 12 '14 at 19:33
1

java.time

The modern Date-Time API, released with Java-8 in March 2014, supplanted the legacy date-time API (the java.util Date-Time API and their formatting API, SimpleDateFormat). Since then, it is strongly recommended to stop using the error-prone legacy API and switch to java.time API.

Solution using java.time API: java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format (e.g. your date-time string, 1993-06-08T18:27:02.000Z).

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;

class Main {
    public static void main(String[] args) {
        String strModifiedDate = "1993-06-08T18:27:02.000Z";
        Instant instant = Instant.parse(strModifiedDate);
        System.out.println(instant);

        // It can also be directly parsed into a ZonedDateTime
        ZonedDateTime zdt = ZonedDateTime.parse(strModifiedDate);
        System.out.println(zdt);

        // or even into an OffsetDateTime
        OffsetDateTime odt = OffsetDateTime.parse(strModifiedDate);
        System.out.println(odt);
    }
}

Output:

1993-06-08T18:27:02Z
1993-06-08T18:27:02Z
1993-06-08T18:27:02Z

If for any reason, you need java.util.Date instance, you can get it as follows:

Date date = Date.from(instant);

Learn more about the modern Date-Time API from Trail: Date Time.

Just for the sake of completeness:

Here is the solution using the legacy date-time API. First of all, note that 'Z' is not the same as Z. 'Z' is just a character literal whereas Z is the timezone designator for zero-timezone offset (or UTC). So, never use 'Z' in the date-time formatting/parsing pattern; otherwise, it will be interpreted merely as a literal character, and not as a timezone designator.

The correct letter to be used in the pattern for timezone offset is X.

Demo:

class Main {
    public static void main(String[] args) throws ParseException {
        String strModifiedDate = "1993-06-08T18:27:02.000Z";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        Date modifiedDate = sdf.parse(strModifiedDate);
        System.out.println(modifiedDate);
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

The answer by Sotirios Delimanolis is correct. The Z means Zulu time, a zero offset from UTC (+00:00). In other words, not adjusted to any time zone.

Joda-Time

FYI, the Joda-Time library make this work much easier, as does the new java.time package in Java 8.

The format you are using is defined by the ISO 8601 standard. Joda-Time and java.time both parse & generate ISO 8601 strings by default.

A DateTime in Joda-Time knows its own assigned time zone. So as part of the process of parsing, specify a time zone to adjust.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = new DateTime( "1993-06-08T18:27:02.000Z", timeZone );
String output = dateTime.toString();

You can keep the DateTime object in Universal Time if desired.

DateTime dateTime = new DateTime( "1993-06-08T18:27:02.000Z", DateTimeZone.UTC );

When required by other classes, you can generate a java.util.Date object.

java.util.Date date = dateTime.toDate();
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154