Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java utc time in microseconds and in 15 microseconds block

Is there a way to extract java utc time in microseconds after midnight and in 15 microseconds blocks?

Meaning that time from 1 microsecond to 15 microsecond would be represented by the same timing.

like image 558
user1675985 Avatar asked Mar 28 '26 08:03

user1675985


1 Answers

The only issue is with time resolution. In general Java supports millisecond precision via java.util.Date or System.currentTimeMillis(). If you need greater precision, System.nanoTime() might do the trick.

But this brings another issue - nanoTime() is not related to any calendar, it's just an ever growing counter with nanosecond precision. The only workaround I can think of is:

  • determine System.nanoTime() value at midnight, call it S. This might be problematic, but you don't have to wait until midnight, some simple math is enough.

  • calculate the following:

    (System.nanoTime() - S) / 15000
    
  • if the value is 0, we are in the same 15-microsecond range

Are you sure JVM and your program can actually take advantage of such small timings?

like image 183
Tomasz Nurkiewicz Avatar answered Mar 29 '26 22:03

Tomasz Nurkiewicz