Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda