Regarding date dependent unit tests I am figuring out the right way of creating a Java 8 Clock for a specific date. According to the approach proposed in Unit testing a class with a Java 8 Clock, I am trying it with the Clock.fixed
method. However, I am not realizing a short way of doing it.
Is this the right and best way of creating a Java 8 Clock for a specific date?
version 2 (according to suggestions of @Meno, @LakiGeri and @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 now = 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());
}