Check If Date Is Within Supplied Range Using Joda Time

Supposed you need to know if certain date is within a given range of dates, here is how you do it using JodaTime:

//initialize a start & end time variable
DateTime start = new DateTime(1434931200000);
DateTime end = new DateTime(1435276800000);

//supply the start and end to an interval object
Interval interval = new Interval(start, end);

//this is the time that we need to check
DateTime timeToTest = new DateTime(1435046200000);

//now, check if the time is indeed within out specified start and end range
if(interval.contains(timeToTest)){
    System.out.println("Time is within range.");
} else {
    System.out.println("Time is outside the defined range.");    
}

Share