java.time
You are using old outmoded date-time classes that have proven to be poorly designed, confusing, and troublesome. Use java.time instead.
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
. The Joda-Time team also advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
ISO 8601
Your input string complies with ISO 8601 standard. These standard formats are used by default in java.time classes. So no need to specify a formatting pattern.
Offset
Your input value has an offset-from-UTC, a number of 8 hours behind UTC.
The OffsetDateTime
class represents such values.
OffsetDateTime odt = OffsetDateTime.parse( "2016-01-01T10:30:00-0800" );
Time zone
A time zone is an offset-from-UTC plus a set of rules for handling anomalies such as Daylight Saving Time (DST). If you are certain of the time zone intended as the context for this value, apply it to get a ZonedDateTime
.
Specify a time zone (ZoneId
) by a proper time zone name in format of continent/region
. Never use the 3-4 letter abbreviations such as EST
or IST
as they are not true time zones, are not standardized, and are not even unique(!).
ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdt = odt.atZone( zoneId );
Change time zone
If you want to see this value through the lens of local time in India, you can apply a different time zone to create a new ZonedDateTime
. The java.time classes use the immutable objects pattern. So note that rather than alter (“mutate”) the time zone setting on an existing object, the java.time classes create a new object with some of its members based on the original object’s members.
ZonedDateTime zdt_Kolkata = zdt.withZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) ) ;