Joda Time Interval by Examples in Java

Tags: Joda Time Joda Interval Joda DateTime Joda Duration Joda Period Joda DateTimeZone Joda DateTimeUtils Joda Chronology Joda GregorianChronology Joda EthiopicChronology

In this Java Joda-Time tutorial, we learn how to use the org.joda.time.Interval class of Joda-Time library by different example Java programs.

Add Joda Time library to your Java project

To use Joda Time Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'joda-time', name: 'joda-time', version: '2.10.9'

To use Joda Time Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.9</version>
</dependency>

To download the Joda Time .jar file you can visit Joda Time releases page at github.com/JodaOrg/joda-time

What is org.joda.time.Interval class used for?

The Interval class is an immutable class that represents a period of time between two instants. The Intervals are inclusive of the start instant and exclusive of the end instant. And the end instant is always greater than or equal to the start instant.

Create Interval object using constructors

You can create a new Interval object from start and end time in milliseconds.

JodaTimeIntervalExample1.java

import org.joda.time.Chronology;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeIntervalExample1 {
    public static void main(String[] args) {
        long currentTimeMilliseconds = DateTimeUtils.currentTimeMillis();
        long nextOneMinuteMilliseconds = currentTimeMilliseconds + 60000; // 60000 milliseconds = 60 seconds = 1 minute
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();

        Interval interval1 = new Interval(currentTimeMilliseconds, nextOneMinuteMilliseconds);
        Interval interval2 = new Interval(currentTimeMilliseconds, nextOneMinuteMilliseconds, dateTimeZone);
        Interval interval3 = new Interval(currentTimeMilliseconds, nextOneMinuteMilliseconds, chronology);

        System.out.println("New Interval 1: " + interval1);
        System.out.println("New Interval 2: " + interval2);
        System.out.println("New Interval 3: " + interval3);
    }
}
The output is:
New Interval 1: 2021-01-22T20:54:10.498+07:00/2021-01-22T20:55:10.498+07:00
New Interval 2: 2021-01-22T13:54:10.498Z/2021-01-22T13:55:10.498Z
New Interval 3: 2021-01-22T20:54:10.498+07:00/2021-01-22T20:55:10.498+07:00

You can create a new Interval object from start and end time in DateTime type. JodaTimeIntervalExample2.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample2 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);

        Interval interval = new Interval(now, tomorrow);

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T21:00:21.747+07:00/2021-01-23T21:00:21.747+07:00

You can create a new Interval object from a given start DateTime and Duration. JodaTimeIntervalExample3.java

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;

public class JodaTimeIntervalExample3 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        Duration oneMinuteDuration = Duration.standardMinutes(1);

        Interval interval = new Interval(now, oneMinuteDuration);

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T21:02:00.378+07:00/2021-01-22T21:03:00.378+07:00

Or from a Duration and end DateTime. JodaTimeIntervalExample4.java

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;

public class JodaTimeIntervalExample4 {
    public static void main(String[] args) {
        Duration oneMinuteDuration = Duration.standardMinutes(1);
        DateTime endDateTime = DateTime.now();

        Interval interval = new Interval(oneMinuteDuration, endDateTime);

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T21:03:06.443+07:00/2021-01-22T21:04:06.443+07:00

You can create a new Interval object from a given start DateTime object and Period. JodaTimeIntervalExample5.java

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;

public class JodaTimeIntervalExample5 {
    public static void main(String[] args) {
        DateTime startDateTime = DateTime.now();
        Period oneMinutePeriod = Period.minutes(1);

        Interval interval = new Interval(startDateTime, oneMinutePeriod);

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T21:08:50.410+07:00/2021-01-22T21:09:50.410+07:00

Or from a Period and end DateTime object.

JodaTimeIntervalExample6.java

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;

public class JodaTimeIntervalExample6 {
    public static void main(String[] args) {
        Period oneMinutePeriod = Period.minutes(1);
        DateTime endDateTime = DateTime.now();

        Interval interval = new Interval(oneMinutePeriod, endDateTime);

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T21:09:16.752+07:00/2021-01-22T21:10:16.752+07:00

Parse String into Interval

You can use Interval.parse() static method to parse a String of start and end date time to Interval.

JodaTimeIntervalExample7.java

import org.joda.time.Interval;

public class JodaTimeIntervalExample7 {
    public static void main(String[] args) {

        Interval interval = Interval.parse("2021-01-22T08:00:00.000/2021-01-22T10:00:00.000");

        System.out.println("New Interval: " + interval);
    }
}
The output is:
New Interval: 2021-01-22T08:00:00.000+07:00/2021-01-22T10:00:00.000+07:00

Or Interval.parseWithOffset() static method to parse a String of start/end date time with an offset.

JodaTimeIntervalExample8.java

import org.joda.time.Interval;

public class JodaTimeIntervalExample8 {
    public static void main(String[] args) {

        Interval interval1 = Interval.parseWithOffset("2021-01-22T08:00:00.000/P1D");
        Interval interval2 = Interval.parseWithOffset("P1D/2021-01-22T08:00:00.000");

        System.out.println("New Interval 1: " + interval1);
        System.out.println("New Interval 2: " + interval2);
    }
}
The output is:
New Interval 1: 2021-01-22T08:00:00.000+07:00/2021-01-23T08:00:00.000+07:00
New Interval 2: 2021-01-21T08:00:00.000+07:00/2021-01-22T08:00:00.000+07:00

Get the overlap between two Interval objects

You can use the Interval.overlap() method to get the overlap between two Interval objects as a new Interval object.

JodaTimeIntervalExample9.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample9 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = new Interval(tomorrow, nextTwoDays);
        Interval interval3 = new Interval(now, nextTwoDays);

        Interval overlap1 = interval1.overlap(interval2);
        Interval overlap2 = interval1.overlap(interval3);
        Interval overlap3 = interval2.overlap(interval3);

        System.out.println("New Interval 1: " + interval1);
        System.out.println("New Interval 2: " + interval2);
        System.out.println("New Interval 3: " + interval3);

        System.out.println("Interval 1 Overlap Interval 2: " + overlap1);
        System.out.println("Interval 1 Overlap Interval 3: " + overlap2);
        System.out.println("Interval 2 Overlap Interval 3: " + overlap3);
    }
}
The output is:
New Interval 1: 2021-01-22T21:27:12.534+07:00/2021-01-23T21:27:12.534+07:00
New Interval 2: 2021-01-23T21:27:12.534+07:00/2021-01-24T21:27:12.534+07:00
New Interval 3: 2021-01-22T21:27:12.534+07:00/2021-01-24T21:27:12.534+07:00
Interval 1 Overlap Interval 2: null
Interval 1 Overlap Interval 3: 2021-01-22T21:27:12.534+07:00/2021-01-23T21:27:12.534+07:00
Interval 2 Overlap Interval 3: 2021-01-23T21:27:12.534+07:00/2021-01-24T21:27:12.534+07:00

Check two Interval objects whether overlap or not

To check whether an Interval overlap another Interval or not you can use Interval.overlaps() method.

JodaTimeIntervalExample10.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample10 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = new Interval(tomorrow, nextTwoDays);
        Interval interval3 = new Interval(now, nextTwoDays);

        boolean isOverlap1 = interval1.overlaps(interval2);
        boolean isOverlap2 = interval1.overlaps(interval3);
        boolean isOverlap3 = interval2.overlaps(interval3);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
        System.out.println("Interval 3: " + interval3);

        System.out.println("Is Interval 1 Overlap Interval 2: " + isOverlap1);
        System.out.println("Is Interval 1 Overlap Interval 3: " + isOverlap2);
        System.out.println("Is Interval 2 Overlap Interval 3: " + isOverlap3);
    }
}
The output is:
Interval 1: 2021-01-22T21:30:15.792+07:00/2021-01-23T21:30:15.792+07:00
Interval 2: 2021-01-23T21:30:15.792+07:00/2021-01-24T21:30:15.792+07:00
Interval 3: 2021-01-22T21:30:15.792+07:00/2021-01-24T21:30:15.792+07:00
Is Interval 1 Overlap Interval 2: false
Is Interval 1 Overlap Interval 3: true
Is Interval 2 Overlap Interval 3: true

Get the gap between two Interval objects

JodaTimeIntervalExample11.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample11 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);
        DateTime nextThreeDays = now.plusDays(3);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = new Interval(nextTwoDays, nextThreeDays);

        Interval gapInterval = interval1.gap(interval2);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
        System.out.println("Gap Interval: " + gapInterval);
    }
}
The output is:
Interval 1: 2021-01-22T21:38:22.657+07:00/2021-01-23T21:38:22.657+07:00
Interval 2: 2021-01-24T21:38:22.657+07:00/2021-01-25T21:38:22.657+07:00
Gap Interval: 2021-01-23T21:38:22.657+07:00/2021-01-24T21:38:22.657+07:00

Check the Interval abuts with another Interval object

JodaTimeIntervalExample12.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample12 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = new Interval(tomorrow, nextTwoDays);
        Interval interval3 = new Interval(now, nextTwoDays);

        boolean isAbuts1 = interval1.abuts(interval2);
        boolean isAbuts2 = interval1.abuts(interval3);
        boolean isAbuts3 = interval2.abuts(interval3);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
        System.out.println("Interval 3: " + interval3);

        System.out.println("Is Interval 1 Abuts Interval 2: " + isAbuts1);
        System.out.println("Is Interval 1 Abuts Interval 3: " + isAbuts2);
        System.out.println("Is Interval 2 Abuts Interval 3: " + isAbuts3);
    }
}
The output is:
Interval 1: 2021-01-22T22:15:04.624+07:00/2021-01-23T22:15:04.624+07:00
Interval 2: 2021-01-23T22:15:04.624+07:00/2021-01-24T22:15:04.624+07:00
Interval 3: 2021-01-22T22:15:04.624+07:00/2021-01-24T22:15:04.624+07:00
Is Interval 1 Abuts Interval 2: true
Is Interval 1 Abuts Interval 3: false
Is Interval 2 Abuts Interval 3: false

How to use Interval.withChronology() method

JodaTimeIntervalExample13.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.chrono.EthiopicChronology;

public class JodaTimeIntervalExample13 {
    public static void main(String[] args) {
        Chronology chronology  = EthiopicChronology.getInstance();

        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withChronology(chronology);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:20:23.640+07:00/2021-01-23T22:20:23.640+07:00
Interval 2: 2013-05-14T22:20:23.640+07:00/2013-05-15T22:20:23.640+07:00

How to use Interval.withStartMillis() method

JodaTimeIntervalExample14.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample14 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withStartMillis(System.currentTimeMillis());

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:22:23.440+07:00/2021-01-23T22:22:23.440+07:00
Interval 2: 2021-01-22T22:22:23.485+07:00/2021-01-23T22:22:23.440+07:00

How to use Interval.withStart() method

JodaTimeIntervalExample15.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample15 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime oneDayAgo = now.minusDays(1);
        DateTime tomorrow = now.plusDays(1);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withStart(oneDayAgo);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:24:05.564+07:00/2021-01-23T22:24:05.564+07:00
Interval 2: 2021-01-21T22:24:05.564+07:00/2021-01-23T22:24:05.564+07:00

How to use Interval.withEndMillis() method

JodaTimeIntervalExample16.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample16 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withEndMillis(nextTwoDays.getMillis());

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:26:27.912+07:00/2021-01-23T22:26:27.912+07:00
Interval 2: 2021-01-22T22:26:27.912+07:00/2021-01-24T22:26:27.912+07:00

How to use Interval.withEnd() method

JodaTimeIntervalExample17.java

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class JodaTimeIntervalExample17 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        DateTime nextTwoDays = now.plusDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withEnd(nextTwoDays);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:27:41.176+07:00/2021-01-23T22:27:41.176+07:00
Interval 2: 2021-01-22T22:27:41.176+07:00/2021-01-24T22:27:41.176+07:00

How to use Interval.withDurationAfterStart() method

JodaTimeIntervalExample18.java

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;

public class JodaTimeIntervalExample18 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        Duration twoDaysDuration = Duration.standardDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withDurationAfterStart(twoDaysDuration);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:29:57.967+07:00/2021-01-23T22:29:57.967+07:00
Interval 2: 2021-01-22T22:29:57.967+07:00/2021-01-24T22:29:57.967+07:00

How to use Interval.withDurationBeforeEnd() method

JodaTimeIntervalExample19.java

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;

public class JodaTimeIntervalExample19 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        Duration twoDaysDuration = Duration.standardDays(2);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withDurationBeforeEnd(twoDaysDuration);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:31:39.693+07:00/2021-01-23T22:31:39.693+07:00
Interval 2: 2021-01-21T22:31:39.693+07:00/2021-01-23T22:31:39.693+07:00

How to use Interval.withPeriodAfterStart() method

JodaTimeIntervalExample20.java

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;

public class JodaTimeIntervalExample20 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        Period threeDaysPeriod = Period.days(3);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withPeriodAfterStart(threeDaysPeriod);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:33:48.166+07:00/2021-01-23T22:33:48.166+07:00
Interval 2: 2021-01-22T22:33:48.166+07:00/2021-01-25T22:33:48.166+07:00

How to use Interval.withPeriodBeforeEnd() method

JodaTimeIntervalExample21.java

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;

public class JodaTimeIntervalExample21 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime tomorrow = now.plusDays(1);
        Period threeDaysPeriod = Period.days(3);

        Interval interval1 = new Interval(now, tomorrow);
        Interval interval2 = interval1.withPeriodBeforeEnd(threeDaysPeriod);

        System.out.println("Interval 1: " + interval1);
        System.out.println("Interval 2: " + interval2);
    }
}
The output is:
Interval 1: 2021-01-22T22:34:26.069+07:00/2021-01-23T22:34:26.069+07:00
Interval 2: 2021-01-20T22:34:26.069+07:00/2021-01-23T22:34:26.069+07:00

Happy Coding 😊

Joda Time Instant by Examples in Java

Joda Time LocalDate by Examples in Java

Joda Time LocalTime by Examples in Java

Joda Time LocalDateTime by Examples in Java

Joda Time DateTime by Examples in Java

Joda Time Duration by Examples in Java

Joda Time Period by Examples in Java