By Pradip Borde


2013-10-01 09:18:10 8 Comments

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?

6 comments

@Basil Bourque 2016-03-09 07:05:21

tl;dr

The other Answers are outmoded as of Java 8.

Instant                           // Represent a moment in UTC. 
.parse( "2013-09-29T18:46:19Z" )  // Parse text in standard ISO 8601 format where the `Z` means UTC, pronounces “Zulu”.
.atZone(                          // Adjust from UTC to a time zone. 
    ZoneId.of( "Asia/Kolkata" )
)                                 // Returns a `ZonedDateTime` object. 

ISO 8601

Your string format happens to comply with the ISO 8601 standard. This standard defines sensible formats for representing various date-time values as text.

java.time

The old java.util.Date/.Calendar and java.text.SimpleDateFormat classes have been supplanted by the java.time framework built into Java 8 and later. See Tutorial. Avoid the old classes as they have proven to be poorly designed, confusing, and troublesome.

Part of the poor design in the old classes has bitten you, where the toString method applies the JVM's current default time zone when generating a text representation of the date-time value that is actually in UTC (GMT); well-intentioned but confusing.

The java.time classes use ISO 8601 formats by default when parsing/generating textual representations of date-time values. So no need to specify a parsing pattern.

An Instant is a moment on the timeline in UTC.

Instant instant = Instant.parse( "2013-09-29T18:46:19Z" );

You can apply a time zone as needed to produce a ZonedDateTime object.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );

@Thirdman 2017-02-15 15:20:42

and if you don't have the option to go on java8 better use 'yyyy-MM-dd'T'HH:mm:ssXXX' as this gets correctly parsed again (while with only one X this may not be the case... depending on your parsing function)

X generates: +01

XXX generates: +01:00

@AZ_ 2015-01-22 03:29:30

From ISO 8601 String to Java Date Object

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
sdf.parse("2013-09-29T18:46:19Z"); //prints-> Mon Sep 30 02:46:19 CST 2013

if you don't set TimeZone.getTimeZone("GMT") then it will output Sun Sep 29 18:46:19 CST 2013

From Java Date Object to ISO 8601 String

And to convert Dateobject to ISO 8601 Standard (yyyy-MM-dd'T'HH:mm:ss'Z') use following code

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));           
System.out.println(sdf.format(new Date())); //-prints-> 2015-01-22T03:23:26Z

Also note that without ' ' at Z yyyy-MM-dd'T'HH:mm:ssZ prints 2015-01-22T03:41:02+0000

@Mansuu.... 2018-03-19 10:54:42

It gives me java.text.ParseException: Unparseable date: "2018-05-01T18:30:00.000Z" @AZ_

@MyTitle 2015-04-07 22:12:06

IF you want to handle 'standard' JSON representation of the Date then better to use this pattern: "yyyy-MM-dd'T'HH:mm:ssX".

Notice the X on the end. It will handle timezones in ISO 8601 standard, and ISO 8601 is exactly what produces this statement in Javascript new Date().toJSON()

Comparing to other answers it has some benefits:

  1. You don't need to require your clients to send date in GMT
  2. You don't need to explicitly convert your Date object to GMT using this: sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

@transang 2016-02-03 00:03:52

I am using play's automatic form binding, and use @Format.DateTime annotation. Use default pattern with Z at ending seem be incorrect. After changed to X, it works. Many thanks

@Lambart 2017-12-04 21:32:16

If your date specifies the timezone in an RFC822-compliant fashion like "-0500", this works fine. But for a date such as "2013-07-15T10:22:17-05:00" (also valid ISO8601 TZ) this breaks. In that case you need to use "yyyy-MM-dd'T'HH:mm:ssXXX".

@Peter Lawrey 2013-10-01 11:41:56

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"));

@binarygiant 2014-10-01 19:59:00

If you have a date, say 2012-12-06T06:00:00 without the Z, does that represent GMT?

@Peter Lawrey 2014-10-01 21:48:25

@binarygiant You would have to ask those who sent it. It could be the local time of the sender.

@Prashanth 2015-06-30 09:13:51

Timezone can be set to "UTC"

@Norris Boateng 2017-12-19 17:02:42

This works just fine. Thanks

@kisna 2018-03-15 19:35:24

Instead of setting the timezone in so many places for different libraries and avoid conflicts with your machine/laptop timezone, you should always set the default timezone of the JVM by setting user.timezone system property: java -Duser.timezone=GMT ...

@Subir Kumar Sao 2013-10-01 09:23:14

'T' and 'Z' are considered here as constants. You need to pass Z without the quotes. Moreover you need to specify the timezone in the input string.

Example : 2013-09-29T18:46:19-0700 And the format as "yyyy-MM-dd'T'HH:mm:ssZ"

@Pradip Borde 2013-10-01 10:32:47

Subir, Thanks for your answer, but what is "0700" appended to input string??

@Subir Kumar Sao 2013-10-01 11:57:44

-0700 is the time zone ie -7:00 Hrs

@Tastybrownies 2016-11-16 21:28:47

for -07:00 hour and not -0700 it would be the following: yyyy-MM-dd'T'HH:mm:ss.SSSXXX

Related Questions

Sponsored Content

24 Answered Questions

19 Answered Questions

[SOLVED] Create a Date with a set timezone without using a string representation

12 Answered Questions

[SOLVED] simpledateformat parsing date with 'Z' literal

4 Answered Questions

[SOLVED] Java timezone - strange behavior with IST?

  • 2012-06-29 15:32:56
  • Vasan
  • 12112 View
  • 3 Score
  • 4 Answer
  • Tags:   java timezone

5 Answered Questions

2 Answered Questions

[SOLVED] Convert string to appropriate date with timezone java

  • 2014-05-15 07:55:26
  • minu
  • 10302 View
  • 7 Score
  • 2 Answer
  • Tags:   java date timezone

2 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Java SimpleDateFormat timezone parsing

  • 2013-07-10 21:21:11
  • Stanislav Palatnik
  • 8105 View
  • 4 Score
  • 1 Answer
  • Tags:   java timezone

1 Answered Questions

[SOLVED] Java SimpleDateFormat Wrong Timezone after parsing

  • 2011-10-31 10:50:18
  • IgorDiy
  • 4763 View
  • 6 Score
  • 1 Answer
  • Tags:   java date timezone

Sponsored Content