How to create a Java 8 Clock for a specific date -
regarding date dependent unit tests figuring out right way of creating java 8 clock specific date. according approach proposed in unit testing class java 8 clock, trying clock.fixed
method. however, not realizing short way of doing it.
is right , best way of creating java 8 clock specific date?
version 2 (according suggestions of @meno, @lakigeri , @basil-bourque)
private static clock utcclockof(int year, int month, int day) { zoneoffset offset = zoneoffset.utc; instant inst = localdate .of(year, month, day) .atstartofday(offset) .toinstant(); return clock.fixed(inst, offset.normalized()); }
version 1
static clock clockof(int year, int month, int day) { localdate = localdate.of(year, month, day); long days = now.toepochday(); long secs = days*24*60*60; instant inst = instant.ofepochsecond(secs); return clock.fixed(inst, zoneid.systemdefault()); }
Comments
Post a Comment