Question

Given a ReadableInstant and a ReadableDuration, I want to create another ReadableInstant that represents the point in time that lies the given duration after the given point in time. The simplest way to do this that I found is this but I assume there is some way to do this directly:

ReadableInstant after(ReadableInstant instant, ReadableDuration duration) {
    return new Instant(instant.getMillis() + duration.getMillis());
}

Instant defines a method plus() which takes a ReadableDuration but there are other implementations of ReadableInstant so I can't use that method.

Was it helpful?

Solution

The interface ReadableInstant provides a method toInstant() which returns an Instant. Call that and then call plus() on that Instant.

So:

ReadableInstant instant;
ReadableDuration duration;
// ...
return instant.toInstant().plus(duration);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top