java – How to get UTC + 0 date in Java 8?

tl; dr

Instant.now()

Java.time

The old and troublesome date-time classes bundled with early versions of Java have been supplanted by classes Java.time integrated in Java 8 and later. You see Oracle Tutorial . Much of the functionality has been ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP =.

Instant

A Instant represents a moment on the timeline in UTC with a resolution up to nanoseconds .

Instant instant = Instant.now();

The method toString generates a String object with text representing the date-time value using one of the standard formats ISO 8601 .

String output = instant.toString();  

2016-06-27T19: 15: 25.864Z

The class Instant is a class of building blocks in Java.time. This should be your reference class when handling date and time, as generally the best practice is to track, store, and swap date and time values ​​in UTC.

OffsetDateTime

But Instant has limitations such as no formatting option for generating strings in alternative formats. For more flexibility, convert from Instant to OffsetDateTime . Specify a offset-from-UTC . In Java.time this means an object ZoneOffset . Here we want to stick with UTC (+00) so that we can use the convenient constant ZoneOffset.UTC .

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );

2016-06-27T19: 15: 25.864Z

Or skip the class Instant.

OffsetDateTime.now( ZoneOffset.UTC )

Now with an object OffsetDateTime in hand, you can use DateTimeFormatter to create String objects with text in alternative formats. Search Stack Overflow for many examples of using DateTimeFormatter.

ZonedDateTime

When you want to view time of the wall clock for a particular time zone, apply a ZoneId to get a ZonedDateTime .

In this example we apply the Montreal time zone. In the summer, below Daylight Saving Time (DST) meaningless, the zone has an offset of -04:00. Then notice how the time of day is four hours earlier in the output, 15 instead of 19 hours. Instant And ZonedDateTime they both represent the same simultaneous moment, viewed only through two different lenses.

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

2016-06-27T15: 15: 25,864-04: 00 [America/Montreal]

Conversion

While you should avoid the old date and time classes, you can convert using new methods added to the old classes if you need to. Here we use Java.util.Date.from( Instant ) And Java.util.Date::toInstant .

Java.util.Date utilDate = Java.util.Date.from( instant );

And going in the opposite direction.

Instant instant= utilDate.toInstant();

Likewise, look for new methods added to GregorianCalendar (subclass of Calendar) to convert from ea Java.time.ZonedDateTime.

Table of types of date-time classes in modern Java.time versus legacy.


Information on Java.time

The framework Java.time it is embedded in Java 8 and later. These classes replace the old problems legacy date-time classes such as Java.util.Date , Calendar & SimpleDateFormat .

The project Joda-Time now in maintenance mode recommend migration to Java.time = classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. The specification is JSR 31 .

It is possible to exchange Java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for classes Java.sql.*.

Where to get the Java.time classes?

The project ThreeTen-Extra extends Java.time with additional classes. This project is a test bed for possible future additions to Java.time. You can find some useful classes here like Interval , YearWeek , YearQuarter And other .

Leave a comment