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.
Your string format happens to comply with the ISO 8601 standard. This standard defines sensible formats for representing various date-time values as text.
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 );