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