I have a solution for setting future/past date Java 8, but I would like to know if there is a cleaner way.
I have a method in which one of the argument is of type ZonedDateTime
.
I am extracting the time, converting to milliseconds and subtracting that from present now.
void setFuturePastDate(ZonedDateTime dateTime) {
long diffInSeconds = ZonedDateTime.now().toEpochSecond()
- dateTime.toEpochSecond();
Duration durationInSeconds = Duration.ofSeconds(diffInSeconds);
Instant instantInSeconds = now.minusSeconds(durationInSeconds);
Clock clock = Clock.fixed(instantInSeconds, ZoneId.systemDefault());
System.out.println(ZonedDateTime.now(clock)); // - I have a past date
In Joda it was simple:
setCurrentMillisSystem(long)
and wherever we access new DateTime()
it will give the date set.
Is there a cleaner way in Java 8 ?