简体   繁体   中英

Data type for nanosecond time precision in Java

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 (eg 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM