Comparing the current date & time in Milan with 8 AM today in Milan.
timestamp (ts1) stored in a long, expressed in nanos since epoch
Assuming you mean the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z, derive a Instant
.
Instant instant = Instant.ofEpochSecond( 0 , myNanos ) ;
another timestamp (ts2), which I know is for today 8am
Determining "today" requires a time zone. For any given moment, both the date and time-of-day vary around the globe by time zone.
By the way, your Question’s time zone Europe/Milan
does not exist. I don't know much about Italy, but I assume you really want Europe/Rome
.
ZoneId z = ZoneId.of( "Europe/Rome" ) ;
ZonedDateTime nowInMilan = ZonedDateTime.now( z ) ;
We can move to another moment using the with
method. Pass a LocalTime
object for 8 AM. All parts of time-of-day will be replaced: hour, minute, second, & fractional second.
ZonedDateTime eightAmTodayInMilan = nowInMilan.with( LocalTime.of( 8 , 0 ) ) ;
compare it
You can compare moments using the isAfter
, isBefore
, and isEqual
methods.
boolean nowIsAfter8AmInMilan = nowInMilan.isAfter( eightAmTodayInMilan ) ;
Alternatively, you could calculate elapsed time as a Duration
. Then check if negative or positive.
Duration elapsed = Duration.between( eightAmTodayInMilan , nowInMilan ) ;
boolean nowIsBefore8AmInMilan = elapsed.isNegative() ;
LocalDateTime
cannot represent a moment
The LocalDateTime
represents a date with a time-of-day, no more, no less. This class purposely lacks any concept of time zone or offset-from-UTC.
Without the context of a zone/offset, an instance of LocalDateTime
is inherently ambiguous. We don't know if noon on the 23rd of this January means noon in Tokyo, noon in Toulouse, or noon in Toledo Ohio US — three very different moments, each several hours apart.
For the problem stated in this Question, I would not involve LocalDateTime
.
The Y2262 problem
I would suggest you find another way to represent a moment as a count of nanoseconds.
The main reason is that such a count is inherently ambiguous and error-prone. Since no human can read such a number as a date and time, logging and debugging are complicated.
Also, beware of the Y2262 problem. Assuming your epoch reference is first moment of 1970 in UTC, then counting nanos limits your future 2262-04-11T23:47:16.854775807Z. That is sufficient for current business purposes, but what if your app is still running in a few centuries?
Instant.EPOCH.plus( Duration.ofNanos( Long.MAX_VALUE ) )
Generally best to exchange date-time values as text in standard ISO 8601 format. Those formats are designed to be readable both by machine and by humans across cultures.