6

Is there any standard Java implementation to represent a time range with FROM and TO? Actually I am using the following class.

I also looked at Instant.range() or Period, which are both not defining a time range or span, but calculating the duration of a period instead of saving the exact time range with FROM and TO.

public class TimeRange { private Instant from, to; public TimeRange(Instant from, Instant to) { this.from = from; this.to = to; } public Instant getFrom() { return from; } public void setFrom(Instant from) { this.from = from; } public Instant getTo() { return to; } public void setTo(Instant to) { this.to = to; } public boolean isProperlySet() { return from != null && to != null; } } 
11
  • 4
    The Threeten-extra library contains an Interval class. Does that cover your need? Commented Sep 13, 2017 at 7:50
  • 5
    I find the method isProperlySet() weird. Are you doing this for every object, that contains settable members? Why not just throwing or let throw an Exception? Commented Sep 13, 2017 at 7:51
  • 2
    Or my lib Time4J with even more features, see API. Commented Sep 13, 2017 at 7:52
  • 3
    I agree with @HerrDerb. Verify the received Instants in the constructor, e.g. this.from = Objects.requireNonNull(from);. Commented Sep 13, 2017 at 8:05
  • @Henrik: that won't work because the objects are not final Commented Sep 13, 2017 at 8:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.