Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 577
Michal Špondr Avatar asked Apr 21 '26 00:04

Michal Špondr


1 Answers

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.

like image 63
Robby Cornelissen Avatar answered Apr 22 '26 14:04

Robby Cornelissen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!