The value 2000-01-01T10:00:00Z
is not not at all “useless”. This describes an exact moment on the timeline, with a date, a time-of-day, and an offset-from-UTC which is UTC itself in this case.
ISO 8601
That string format is defined by the ISO 8601 standard. The Z
on the end is short for Zulu
and means UTC.
Instant
You can parse this string directly as a Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds. That means up to nine (9) digits of a decimal fraction.
Instant instant = Instant.parse( "2000-01-01T10:00:00Z" );
The Instant
class is only the basic building-block class in Java. For more flexibility we should either:
- Assign the instant an offset-from-UTC (
ZoneOffset
) to get an OffsetDateTime
.
- Assign the instant a time zone (
ZoneId
) to get a ZonedDateTime
.
What is the difference? A time zone is an offset plus a set of rules for handling anomalies such as Daylight Saving Time (DST).
OffsetDateTime
So if you truly wanted the time-of-day for this moment as seen in UTC, then assign an offset of UTC (ZoneOffset.UTC
) to get an OffsetDateTime
, and then extract a LocalTime
object. LocalTime
represents a time-of-day without a date and without a time zone.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
LocalTime lt = odt.toLocalTime(); // 10:00:00
ZonedDateTime
If you want to see the same moment through the lens of another time zone, to see that particular region’s wall-clock time, specify a ZoneId
. For example, that same moment in Québec would be 5 hours behind UTC.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z ); // 2000-01-01T05:00:00-05:00[America/Montreal]
LocalTime lt = zdt.toLocalTime(); // 05:00:00
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, 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 (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.