Java date parsing with microsecond or nanosecond accuracy
Asked Answered
F

4

35

According to the SimpleDateFormat class documentation, Java does not support time granularity above milliseconds in its date patterns.

So, a date string like

  • 2015-05-09 00:10:23.999750900 // The last 9 digits denote nanoseconds

when parsed via the pattern

  • yyyy-MM-dd HH:mm:ss.SSSSSSSSS // 9 'S' symbols

actually interprets the whole number after the . symbol as (nearly 1 billion!) milliseconds and not as nanoseconds, resulting in the date

  • 2015-05-20 21:52:53 UTC

i.e. over 11 days ahead. Surprisingly, using a smaller number of S symbols still results in all 9 digits being parsed (instead of, say, the leftmost 3 for .SSS).

There are 2 ways to handle this issue correctly:

  • Use string preprocessing
  • Use a custom SimpleDateFormat implementation

Would there be any other way for getting a correct solution by just supplying a pattern to the standard SimpleDateFormat implementation, without any other code modifications or string manipulation?

Flatware answered 9/5, 2015 at 1:31 Comment(1)
... by just supplying a pattern to the standard SimpleDateFormat implementation, without any other code modifications or string manipulation? Obviously not.Lachrymose
P
51

tl;dr

LocalDateTime.parse(                 // With resolution of nanoseconds, represent the idea of a date and time somewhere, unspecified. Does *not* represent a moment, is *not* a point on the timeline. To determine an actual moment, place this date+time into context of a time zone (apply a `ZoneId` to get a `ZonedDateTime`). 
    "2015-05-09 00:10:23.999750900"  // A `String` nearly in standard ISO 8601 format.
    .replace( " " , "T" )            // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
)                                    // Returns a `LocalDateTime` object.

Nope

No, you cannot use SimpleDateFormat to handle nanoseconds.

But your premise that…

Java does not support time granularity above milliseconds in its date patterns

…is no longer true as of Java 8, 9, 10 and later with java.time classes built-in. And not really true of Java 6 and Java 7 either, as most of the java.time functionality is back-ported.

java.time

SimpleDateFormat, and the related java.util.Date/.Calendar classes are now outmoded by the new java.time package found in Java 8 (Tutorial).

The new java.time classes support nanosecond resolution. That support includes parsing and generating nine digits of fractional second. For example, when you use the java.time.format DateTimeFormatter API, the S pattern letter denotes a "fraction of the second" rather than "milliseconds", and it can cope with nanosecond values.

Instant

As an example, the Instant class represents a moment in UTC. Its toString method generates a String object using the standard ISO 8601 format. The Z on the end means UTC, pronounced “Zulu”.

instant.toString()  // Generate a `String` representing this moment, using standard ISO 8601 format.

2013-08-20T12:34:56.123456789Z

Note that capturing the current moment in Java 8 is limited to millisecond resolution. The java.time classes can hold a value in nanoseconds, but can only determine the current time with milliseconds. This limitation is due to the implementation of Clock. In Java 9 and later, a new Clock implementation can grab the current moment in finer resolution, depending on the limits of your host hardware and operating system, usually microseconds in my experience.

Instant instant = Instant.now() ;  // Capture the current moment. May be in milliseconds or microseconds rather than the maximum resolution of nanoseconds.

LocalDateTime

Your example input string of 2015-05-09 00:10:23.999750900 lacks an indicator of time zone or offset-from-UTC. That means it does not represent a moment, is not a point on the timeline. Instead, it represents potential moments along a range of about 26-27 hours, the range of time zones around the globe.

Pares such an input as a LocalDateTime object. First, replace the SPACE in the middle with a T to comply with ISO 8601 format, used by default when parsing/generating strings. So no need to specify a formatting pattern.

LocalDateTime ldt = 
        LocalDateTime.parse( 
            "2015-05-09 00:10:23.999750900".replace( " " , "T" )  // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
        ) 
;

java.sql.Timestamp

The java.sql.Timestamp class also handles nanosecond resolution, but in an awkward way. Generally best to do your work inside java.time classes. No need to ever use Timestamp again as of JDBC 4.2 and later.

myPreparedStatement.setObject( … , instant ) ;

And retrieval.

Instant instant = myResultSet.getObject( … , Instant.class ) ;

OffsetDateTime

Support for Instant is not mandated by the JDBC specification, but OffsetDateTime is. So if the above code fails with your JDBC driver, use the following.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ; 
myPreparedStatement.setObject( … , odt ) ;

And retrieval.

Instant instant = myResultSet.getObject( … , OffsetDateTime.class ).toInstant() ;

If using an older pre-4.2 JDBC driver, you can use toInstant and from methods to go back and forth between java.sql.Timestamp and java.time. These new conversion methods were added to the old legacy classes.

Table of date-time types in Java (both legacy and modern) and in standard SQL


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

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

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

You may 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 java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Petaliferous answered 9/5, 2015 at 3:50 Comment(5)
Yes, although the requirement was for a SimpleDateFormat solution, due to the corresponding use case.Flatware
@OleV.V. Done. Thanks for suggestion.Petaliferous
Anyone looking to parse a datetime string with timezone in it, you can use instant.Parse("datetimestring")Mure
@PrashantSaraswat No, for a time zone, use ZonedDateTime, not Instant. See my table graphic in my Answer.Petaliferous
@BasilBourque Thanks for pointing this out. Both work. I used instant because thats what the javadoc for LocalDateTime suggested.Mure
B
17

I found the it wonderful to cover multiple variants of date time format like this:

final DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
dtfb.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS"))
    .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
    .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS"))
    .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"))
    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
    .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
    .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0);
Brazell answered 31/12, 2019 at 16:5 Comment(1)
Thank you for this!!! Spent quite some time looking at various more complicated solutions for handling variable input and this was exactly what worked!Sago
T
2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API.

You can build a DateTimeFormatter using DateTimeFormatterBuilder as shown below:

DateTimeFormatter parser = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .toFormatter(Locale.ENGLISH);

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    DateTimeFormatter parser = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .toFormatter(Locale.ENGLISH);

    // Test
    Stream.of(
        "2015-05-09 00:10:23.123456789",
        "2015-05-09 00:10:23.12345678",
        "2015-05-09 00:10:23.1234567",
        "2015-05-09 00:10:23.123456",
        "2015-05-09 00:10:23.12345",
        "2015-05-09 00:10:23.1234",
        "2015-05-09 00:10:23.123",
        "2015-05-09 00:10:23.12",
        "2015-05-09 00:10:23.1",
        "2015-05-09 00:10:23.1",
        "2015-05-09 00:10:23",
        "2015-05-09 00:10"
      )
      .map(s -> LocalDateTime.parse(s, parser))
      .forEach(System.out::println);
  }
}

Output:

2015-05-09T00:10:23.123456789
2015-05-09T00:10:23.123456780
2015-05-09T00:10:23.123456700
2015-05-09T00:10:23.123456
2015-05-09T00:10:23.123450
2015-05-09T00:10:23.123400
2015-05-09T00:10:23.123
2015-05-09T00:10:23.120
2015-05-09T00:10:23.100
2015-05-09T00:10:23.100
2015-05-09T00:10:23
2015-05-09T00:10

If you want to make the time part optional, you can use the following DateTimeFormatter:

DateTimeFormatter parser = new DateTimeFormatterBuilder()
    .append(DateTimeFormatter.ISO_LOCAL_DATE)
    .optionalStart()
    .appendLiteral(' ')
    .appendOptional(DateTimeFormatter.ISO_LOCAL_TIME)
    .optionalEnd()
    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
    .toFormatter(Locale.ENGLISH);

Demo:

public class Main {
  public static void main(String[] args) {
    DateTimeFormatter parser = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .optionalStart()
        .appendLiteral(' ')
        .appendOptional(DateTimeFormatter.ISO_LOCAL_TIME)
        .optionalEnd()
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
        .toFormatter(Locale.ENGLISH);

    // Test
    Stream.of(
        "2015-05-09 00:10:23.123456789",
        "2015-05-09 00:10:23.12345678",
        "2015-05-09 00:10:23.1234567",
        "2015-05-09 00:10:23.123456",
        "2015-05-09 00:10:23.12345",
        "2015-05-09 00:10:23.1234",
        "2015-05-09 00:10:23.123",
        "2015-05-09 00:10:23.12",
        "2015-05-09 00:10:23.1",
        "2015-05-09 00:10:23.1",
        "2015-05-09 00:10:23",
        "2015-05-09 00:10",
        "2015-05-09"
        )
        .map(s -> LocalDateTime.parse(s, parser))
        .forEach(System.out::println);
  }
}

Output:

2015-05-09T00:10:23.123456789
2015-05-09T00:10:23.123456780
2015-05-09T00:10:23.123456700
2015-05-09T00:10:23.123456
2015-05-09T00:10:23.123450
2015-05-09T00:10:23.123400
2015-05-09T00:10:23.123
2015-05-09T00:10:23.120
2015-05-09T00:10:23.100
2015-05-09T00:10:23.100
2015-05-09T00:10:23
2015-05-09T00:10
2015-05-09T00:00

Learn more about the modern Date-Time API from Trail: Date Time.

Topgallant answered 30/10, 2022 at 21:14 Comment(0)
F
2

TEST CODE:

@Test
void contextLoads() throws ParseException {
    DateTimeFormatter ISO_LOCAL_DATE = new >DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .optionalStart().appendLiteral('/').optionalEnd()
            .optionalStart().appendLiteral('-').optionalEnd()
            .optionalStart().appendValue(MONTH_OF_YEAR, 2)
            .optionalStart().appendLiteral('/').optionalEnd()
            .optionalStart().appendLiteral('-').optionalEnd()
            .optionalStart().appendValue(DAY_OF_MONTH, 2)
            .toFormatter();

    DateTimeFormatter ISO_LOCAL_TIME = new DateTimeFormatterBuilder()
            .appendValue(HOUR_OF_DAY, 2)
            .optionalStart().appendLiteral(':').appendValue(MINUTE_OF_HOUR, 2)
            .optionalStart().appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2)
            .optionalStart().appendFraction(NANO_OF_SECOND, 0, 9, true)
            .optionalStart().appendZoneId()
            .toFormatter();

    DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
            .append(ISO_LOCAL_DATE)
            .optionalStart().appendLiteral(' ').optionalEnd()
            .optionalStart().appendLiteral('T').optionalEnd()
            .optionalStart().appendOptional(ISO_LOCAL_TIME).optionalEnd()
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
            .toFormatter(Locale.SIMPLIFIED_CHINESE);

    Stream.of("2015-05-09T00:10:23.934596635Z",
            "2015-05-09 00:10:23.123456789UTC",
            "2015/05/09 00:10:23.123456789",
            "2015-05-09 00:10:23.12345678",
            "2015/05/09 00:10:23.1234567",
            "2015-05-09T00:10:23.123456",
            "2015-05-09 00:10:23.12345",
            "2015/05-09T00:10:23.1234",
            "2015-05-09 00:10:23.123",
            "2015-05-09 00:10:23.12",
            "2015-05-09 00:10:23.1",
            "2015-05-09 00:10:23",
            "2015-05-09 00:10",
            "2015-05-09 01",
            "2015-05-09"
    ).forEach(s -> {
        LocalDateTime date = LocalDateTime.parse(s, dateTimeFormatter);
        System.out.println(s + " ==> " + date);
    });
}

OUT:

2015-05-09T00:10:23.934596635Z ==> 2015-05-09T00:10:23.934596635
2015-05-09 00:10:23.123456789UTC ==> 2015-05-09T00:10:23.123456789
2015/05/09 00:10:23.123456789 ==> 2015-05-09T00:10:23.123456789
2015-05-09 00:10:23.12345678 ==> 2015-05-09T00:10:23.123456780
2015/05/09 00:10:23.1234567 ==> 2015-05-09T00:10:23.123456700
2015-05-09T00:10:23.123456 ==> 2015-05-09T00:10:23.123456
2015-05-09 00:10:23.12345 ==> 2015-05-09T00:10:23.123450
2015/05-09T00:10:23.1234 ==> 2015-05-09T00:10:23.123400
2015-05-09 00:10:23.123 ==> 2015-05-09T00:10:23.123
2015-05-09 00:10:23.12 ==> 2015-05-09T00:10:23.120
2015-05-09 00:10:23.1 ==> 2015-05-09T00:10:23.100
2015-05-09 00:10:23 ==> 2015-05-09T00:10:23
2015-05-09 00:10 ==> 2015-05-09T00:10
2015-05-09 01 ==> 2015-05-09T01:00
2015-05-09 ==> 2015-05-09T00:00
Furring answered 22/5, 2023 at 14:42 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Destructionist

© 2022 - 2024 — McMap. All rights reserved.