3

I can see that GMT time is having 'Z' constant as an indicator that it is GMT time. However when I parse the GMT string, it still is printing local time.

Code:

SimpleDateFormat outFormat = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String timeGMT = "2015-05-21T08:42:27.334Z";
try {
    System.out.println("Time GMT>>>>>>"+outFormat.parse(timeGMT));
} catch (ParseException e) {
    e.printStackTrace();
}

OUTPUT:

Thu May 21 08:42:27 IST 2015

Expected:

Thu May 21 08:42:27 GMT 2015
sjain
  • 23,126
  • 28
  • 107
  • 185
  • This duplicate question explains it very well: http://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone – Georgi Mihaylov May 21 '15 at 08:54
  • Agreed. But what about `Z` constant. It should be treated as `GMT` by format. Then why we need to set the timezone. – sjain May 21 '15 at 09:02
  • Please check this out: http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date – Georgi Mihaylov May 21 '15 at 09:09

2 Answers2

3

Two issues here.

The first is that you are using the wrong format to parse. Your format tells the parser to just look at the Z as a literal character with no significance.

This means it will parse it as a local date because it doesn't treat the Z as a marker of time zone. If you want the Z to be interpreted as time zone, your format should have X instead of 'Z' in it:

    String timeGMT = "2015-05-21T08:42:27.334Z";

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

    Date d1 = f1.parse(timeGMT);
    Date d2 = f2.parse(timeGMT);

    System.out.println(d1);
    System.out.println(d2);

I'm currently in GMT+3, and this is the output I'm getting from this:

Thu May 21 08:42:27 IDT 2015
Thu May 21 11:42:27 IDT 2015

As you see, d2 is 3 hours ahead, which means that it interpreted the original time as in GMT.

Your other problem is that you print the resulting date in your default format. The default format is in your local time zone, so it will print it, as it did mine, in the local zone.

To change this you also have to format the output:

    DateFormat f3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    f3.setTimeZone(TimeZone.getTimeZone("GMT"));

    System.out.println(f3.format(d2));

This yields - for the previous example - the following:

2015-05-21 08:42:27 GMT
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • One line - `"If you want the Z to be interpreted as time zone, your format should have X instead of 'Z'`. +1. – sjain May 21 '15 at 09:24
0

Z in your input is NOT a literal constant but means UTC+00:00 (alias "GMT"). Please remove the apostrophs from your pattern around the last pattern symbol. And use "X" instead of "Z" as symbol in order to correctly interprete "Z" as ISO-8601-marker for the offset UTC+00:00.

SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126