Data type for nanosecond time precision in Java

1k views Asked by At

I would like to have a numeric variable that contains integer number with nanosecond precision. I tried this:

Instant t = Instant.now();
a = BigDecimal.valueof(t.getEpochSecond() * 1e9 + t.getNano());
b = BigDecimal.valueof(t.getEpochSecond() * 1e9 + t.getNano() + 1);

Both a and b are contains same value (e.g. 1.60681511777265408E+18), the +1 operation is lost here.

Any idea how to deal with this precision? The goal of it is to keep nanosecond-precision timestamp for time column in InfluxDB. I understand it has something to do with double precision (Java BigDecimal difference), but I haven't found any working solution yet.

2

There are 2 answers

0
Sweeper On

Just do the operations with the methods in BigDecimal. And make sure you don't use valueOf(double). You can use valueOf(long), however.

BigDecimal a = BigDecimal.valueOf(t.getEpochSecond())
            .multiply(BigDecimal.valueOf(1_000_000_000))
            .add(BigDecimal.valueOf(t.getNano()));

BigDecimal b = BigDecimal.valueOf(t.getEpochSecond())
            .multiply(BigDecimal.valueOf(1_000_000_000))
            .add(BigDecimal.valueOf(t.getNano()))
            .add(BigDecimal.ONE);

Printing a and b gives something like:

1606815981554921000
1606815981554921001
1
Robby Cornelissen On

If you're storing an integer value, why use a BigDecimal instead of a BigInteger?

import java.time.*;
import java.math.*;

public class MyClass {
    public static void main(String args[]) {
      Instant t = Instant.now();
      BigInteger a = BigInteger.valueOf(t.getEpochSecond() * 1_000_000_000
              + t.getNano());
      BigInteger b = BigInteger.valueOf(t.getEpochSecond() * 1_000_000_000
              + t.getNano() + 1);

      System.out.println(a); // 1606816120696314000
      System.out.println(b); // 1606816120696314001
    }
}

Need to switch to BigInteger math methods in the year 2262 though, because the long argument will start overflowing.