2

I'm using the epoch time format to save date. My problem is Java Long is enough to handle this or should I consider Java BigInteger to handle the epoch time?

Damith Ganegoda
  • 4,100
  • 6
  • 37
  • 46

4 Answers4

4

Assuming you mean UNIX epoch, Java long is more then enough. UNIX epoch is number of seconds since January 1, 1970 and is stored (in UNIX) as a 32-bit int.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
4

Yes, a long is sufficient. But in terms of the best way, consider using native types.

In Java <= 7, java.util.Date is designed for this purpose. It has millisecond precision.

In Java >= 8, java.time.Instant is designed for this purpose. It has nanosecond precision.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 2
    FYI, the [Joda-Time](http://www.joda.org/joda-time/) library commonly used in place of java.util.Date also tracks time by [milliseconds](http://en.m.wikipedia.org/wiki/Millisecond). – Basil Bourque Oct 13 '14 at 08:14
1

In Java you can get the milliseconds since the UNIX Epoch with System.currentTimeMillis() which returns a long, so there's no reason to consider something else.

Sandman
  • 2,577
  • 2
  • 21
  • 32
1

If by epoch time, you mean seconds since 1970, long will of course do the job, as it can represent millis as well up until end of time ;-)

My point is that you can might integer instead. it will represent time in secs since 1970 up to year 2038.

If you don't need to represent time before now, consider using a special format like stated here. This will help you represent a wider future range.

Another option for representing time only after now, is starting the measure since 2021, by subtracting the seconds: nowSecs - 2021Secs.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277