Temporal calculations with Java 8 java.time.* -
let have java.time.instant
- start point of event, , java.time.duration
- duration of event.
can use java.time.period
calculate if instant
belongs period? if no - approach should use define it?
given instant instant
, duration duration
, want check if instant tocheck
belongs interval [instant; instant + duration]
.
for this, can calculate duration between instant
, tocheck
duration.between
. if duration positive , less specified duration instant check in wanted interval.
public static void main(string[] args) { instant instant = instant.now(); duration duration = duration.ofseconds(10); instant tocheck = instant.plusseconds(5); duration d = duration.between(instant, tocheck); if (!d.isnegative() && d.compareto(duration) <= 0) { system.out.println("is in interval!"); } }
Comments
Post a Comment