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.
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.
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?
You may want to look at this question for information on getting the time in microseconds. Microsecond date timing is not supported well on Java.
Java supports the date in milliseconds and on some systems you can get a system time of nanoseconds via System.nanoTime()
. If your system supports nanosecond resolution, it would be fairly easy to write a function which can convert your nanosecond number and round it to a 15 microsecond block.
Java 8 and later makes this task quite easy: Pass alternate implementation of Clock
as optional argument to java.time classes.
Instant.now( // Capture the current moment.
Clock.tick( // Access an alternate implementation of `Clock`.
Clock.systemDefaultZone() , // Start with the regular default implementation.
Duration.of( 10 , ChronoUnit.MICROS ) // Override to increment the “ticking” of the clock by this amount of time. Here we specify 10 microseconds, as 15 microseconds does not divide evenly into a whole second.
)
) // Returns an `Instant` object, the class replacing `java.util.Date`.
2018-04-04T21:55:46.537750Z ← Notice how the last digit of microseconds is always zero.
The modern approach to tracking date-time uses the java.time classes.
These classes take an optional argument of Clock
. This is convenient for when you want an altered or artificial clock during development and testing.
You can implement your own Clock
. But several alternate implementations are bundled with the Clock
class itself. One of these is Clock.tick( clock , duration )
which increments the time by your specified Duration
.
Obtains a clock that returns instants from the specified clock truncated to the nearest occurrence of the specified duration.
The catch is the fractional second amounts much divide evenly, as the doc says:
If it has a part smaller than a whole millisecond, then the whole duration must divide into one second without leaving a remainder. All normal tick durations will match these criteria, including any multiple of hours, minutes, seconds and milliseconds, and sensible nanosecond durations, such as 20ns, 250,000ns and 500,000ns.
Instant.now( clock ) // Override default `Clock`.
So 15 microseconds does not work here, but 10 microseconds does. And if you think about it, doesn't that rule apply to the problem of the Question as well since 15 microseconds chunks starting from zero will not make an even second?
Duration d = Duration.of( 10 , ChronoUnit.MICROS ) ; // Define a span of time by which we want to increment the reporting of the current moment.
Clock clock = Clock.tick( Clock.systemDefaultZone() , d ); // Define a clock that increments by this span of time.
for ( int i = 1 ; i < 100 ; i++ ) {
System.out.println( Instant.now( clock ) ); // Override default `Clock` implementation with our special “chunky” incrementing clock.
// Sleep a moment.
try {
Thread.sleep( 3 ); // Sleep arbitrary amount of time.
} catch ( InterruptedException e ) {
e.printStackTrace();
}
}
Notice how the output lines all have zero in the “ones” column of microseconds, because we asked for reporting the time only my increments of ten microseconds, 0.000010 seconds.
2018-04-04T21:55:46.537750Z
2018-04-04T21:55:46.550280Z
2018-04-04T21:55:46.553570Z
2018-04-04T21:55:46.556980Z
2018-04-04T21:55:46.560850Z
…
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.