Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST

Pradip Borde picture Pradip Borde · Oct 1, 2013 · Viewed 566.7k times · Source

I have SimpleDateFormat constructor as

SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")

and I am parsing string "2013-09-29T18:46:19Z".

I have read that here Z represents the GMT/UTC timezone. but when I print this date on console , It prints IST timezne for the returned date.

Now my question is whether my output is right or wrong?

Answer

Peter Lawrey picture Peter Lawrey · Oct 1, 2013

You haven't set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn't change the value.

Set the timezone to GMT and it will be correct.

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