2

Is there a Java class to represent a date range in a standard library?

Something like this:

import java.time.LocalDate;

public class DateRange {
  private LocalDate startDate;
  private LocalDate endDate;
  
  // Getter and setters
}

When I search for this, I came across this class: com.amazonaws.services.health.model.DateTimeRange But I'm looking for a class in a standard library.

I can write my own class of course. But I thought this kind of a standard class would exist.

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • There are both [`Period` and `Duration`](https://docs.oracle.com/javase/tutorial/datetime/iso/period.html). – Elliott Frisch Jul 30 '21 at 04:14
  • 3
    @ElliottFrisch but they represent amounts of time at any instant, not in a specific time range (with a start and end date) – Matteo NNZ Jul 30 '21 at 04:18
  • The second duplicate link ("[Does Java SE 8 have Pairs or Tuples?](https://stackoverflow.com/questions/24328679/does-java-se-8-have-pairs-or-tuples)") is not exactly answering this question, but since a `Range` is a specialization of a `Pair` (both values are same type, and first value should be "before" second value), it is relevant to the question, given how it describes the reasoning for why `Pair` is not in the Java Runtime Library. – Andreas Jul 30 '21 at 04:26
  • 2
    Stephen Colebourne, the man who leads the JSR 310 *java.time* project, also leads the [*ThreeTen-Extra*](https://www.threeten.org/threeten-extra/) project with many classes that extend the *java.time* functionality. Among those classes is [`LocalDateRange`](https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/LocalDateRange.html) class to hold a pair of `LocalDate` objects, just what you need. Has many handy methods such as `abuts`, `contains`, `encloses`, and so on. For a range of moments (pair of `Instant` objects), see `Interval` class in that same library. – Basil Bourque Jul 30 '21 at 06:22

0 Answers0