Java: how to get the current date in the ISO 8601 SECOND standard

I need to get the current date in ISO 8601 with SS milliseconds and time zone HH: MM

The full date plus hours, minutes, seconds, and decimal fraction of a second: YYYY-MM-DDThh: mm: ss.sTZD (for example, 1997-07-16T19: 20:30. 45 + 01:00 )

see: http://www.w3.org/TR/NOTE-datetime

I tried different approaches, but I can not get the correct milliseconds and time digits:

DateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSZZ"); df.setTimeZone(TimeZone.getTimeZone("UTC")); df.format(new Date()); 

JAVA Result: 2012-11-24T21: 19 : 27.758 + 0000

Apache Result: 2012-11-24T21: 19 : 27.758 + 00: 00 (Apache Commons FastDateFormat)

+4
source share
2 answers

There is actually a class in the JDK that handles ISO8601 parsing and formatting:

 import javax.xml.bind.DatatypeConverter; DatatypeConverter.printDateTime(new GregorianCalendar()) //2012-11-24T22:42:03.142+01:00 
+6
source

TL; dr

Current moment, UTC

Use java.time.Instant to capture the current moment in resolution as well as nanoseconds.

 Instant.now() // Capture the current moment in UTC, with a resolution as fine as nanoseconds. Typically captured in microseconds in Java 9 and later, milliseconds in Java 8. .toString() // Generate a 'String' representing the value of this 'Instant' in standard ISO 8601 format. 

2018-01-23T12: 34: 56.123456Z

Z at the end:

  • means UTC ,
  • equivalent to +00:00 offset ,
  • pronounced as "Zulu", and
  • defined by ISO 8601, as well as other arenas such as the military.

Current moment, specific offset

If for some unusual reason you need to tune to a specific UTC offset , use OffsetDateTime by passing ZoneOffset .

 OffsetDateTime.now( // Capture the current moment. ZoneOffset.ofHours( 1 ) // View the current moment adjusted to the wall-clock time of this specific offset-from-UTC. BTW, better to use time zones, generally, than a mere offset-from-UTC. ) .toString() // Generate a 'String' in standard ISO 8601 format. 

2018-07-08T21: 13: 10,723676 + 01: 00

Current moment truncated to millis

To resolve milliseconds truncation . Specify permission from the ChronoUnit enum.

 OffsetDateTime.now( ZoneOffset.ofHours( 1 ) ) .truncatedTo( // Lop off finer part of the date-time. We want to drop any microseconds or nanoseconds, so truncate to milliseconds. Using the immutable objects pattern, so a separate new 'OffsetDateTime' object is generated based on the original objects values. ChronoUnit.MILLIS // Specify your desired granularity via 'ChronoUnit' enum. ) .toString() 

2018-07-08T21: 13: 10,723 +01: 00

java.time

The java.time framework, built into Java 8 and later, uses ISO 8601 by default when parsing or generating textual representations of date and time values.

Instant is a moment in the UTC timeline with a nanosecond resolution.

 Instant instant = Instant.now(); 

The current moment is captured in milliseconds resolution in Java 8, and in Java 9 and later - as a rule, microsecond resolution, depending on the capabilities of the hardware clock and OS. If you require millisecond resolution, truncate any microseconds or nanoseconds.

 Instant instant = Instant.now().truncatedTo( ChronoUnit.MILLIS ) ; 

But you need a specific shift-from-UTC . Therefore, set ZoneOffset to get the OffsetDateTime value.

 ZoneOffset offset = ZoneOffset( "+01:00" ); OffsetDateTime odt = OffsetDateTime.ofInstant( instant , offset ); 

Again, truncate if you specifically want a resolution of milliseconds, not microseconds or nanoseconds.

 odt = odt.truncatedTo( ChronoUnit.MILLIS ) ; // Replace original 'OffsetDateTime' object with new 'OffsetDateTime' object based on the original but with any micros/nanos lopped off. 

To create an ISO 8601 string, just call toString .

 String output = odt.toString(); 

Reset the console. Pay attention to how the hour +01:00 forward as a correction +01:00 and even rolled after midnight, so the date changed from 12th to 13th.

 System.out.println ( "instant: " + instant + " | offset: " + offset + " | odt: " + odt ); 

instantly: 2016-04-12T23: 12: 24.015Z | Displacement: +01: 00 | odt: 2016-04-13T00: 12: 24.015 +01: 00

zonal

By the way, it is better to use the time zone (if known) than just the offset. The time zone is the history of past, present and future changes in the displacement of people in a particular region.

In java.time, this means using ZoneId to get ZonedDateTime .

 ZoneId zoneId = ZoneId.of( "Europe/Paris" ); ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); 

Or skip Instant as a shortcut.

 ZonedDateTime zdt = ZonedDateTime.now( zoneId ) ; 

Create a String representing the text value of this ZonedDateTime object. The default format is the standard ISO 8601 format, reasonably extended to add the name of the zone in square brackets.

zdt.toString (): 2018-07-08T22: 06: 58.780923 + 02: 00 [Europe / Paris]

For other formats, use the DateTimeFormatter class.


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to the java.time classes.

To learn more, check out the Oracle tutorial . And search for qaru for many examples and explanations. The specification is JSR 310 .

You can exchange java.time objects directly with your database. Use a JDBC driver compatible with JDBC 4.2 or later. No need for strings, no need for java.sql.* .

Where can I get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and others .

+1
source

All Articles