Joda Time Duration by Examples in Java

Tags: Joda Time Joda Duration Joda Instant Joda DateTime Joda Days Joda Hours Joda Minutes Joda Seconds

In this Joda-Time tutorial, we learn how to use the org.joda.time.Duration 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.Duration class use for?

The Duration class is an immutable class that is used to specify a length of time in milliseconds.

How to create Duration objects using constructors

Create a Duration object with a given milliseconds value.

JodaTimeDurationExample1.java

import org.joda.time.Duration;

public class JodaTimeDurationExample1 {
    public static void main(String[] args) {
        Duration duration = new Duration(60000); // 60000 milliseconds = 60 seconds = 1 minute

        System.out.println(duration.toString());
    }
}
The output is:
PT60S

Create a Duration object with a given start time and end time in milliseconds. JodaTimeDurationExample2.java

import org.joda.time.Duration;

public class JodaTimeDurationExample2 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        long end = start + 120000;

        Duration duration = new Duration(start, end);

        System.out.println(duration.toString());
    }
}
The output is:
PT120S

Create a Duration object with a given start time and end time in Instant values.

JodaTimeDurationExample3.java

import org.joda.time.Duration;
import org.joda.time.Instant;

public class JodaTimeDurationExample3 {
    public static void main(String[] args) {
        Instant start = new Instant(1000);
        Instant end = new Instant(2000);

        Duration duration = new Duration(start, end);

        System.out.println(duration.toString());
    }
}
The output is:
PT1S

Parse a String value into Duration object

In the following Java program, we use Duration.parse() static method to parse String into Duration object.

JodaTimeDurationExample4.java

import org.joda.time.Duration;

public class JodaTimeDurationExample4 {
    public static void main(String[] args) {
        Duration duration1 = Duration.parse("PT2.999S"); // 2 seconds 999 milliseconds
        Duration duration2 = Duration.parse("PT5S"); // 5 seconds

        System.out.println(duration1);
        System.out.println(duration2);
    }
}
The output is:
PT2.999S
PT5S

Create Duration object from specified number of days

The following Java program is using Duration.standardDays() static method to create Duration object from specified number of days

JodaTimeDurationExample5.java

import org.joda.time.Duration;

public class JodaTimeDurationExample5 {
    public static void main(String[] args) {
        Duration duration = Duration.standardDays(5);

        System.out.println(duration.toString());
    }
}
The output is:
PT432000S

Create Duration object from specified number of hours

In the following Java example, we use Duration.standardHours() static method to create Duration object from specified number of hours.

JodaTimeDurationExample6.java

import org.joda.time.Duration;

public class JodaTimeDurationExample6 {
    public static void main(String[] args) {
        Duration duration = Duration.standardHours(1);

        System.out.println(duration.toString());
    }
}
The output is:
PT3600S

Create Duration object from specified number of minutes

The following example Java program, we learn how to use the Duration.standardMinutes() static method to create Duration object from specified number of minutes.

JodaTimeDurationExample7.java

import org.joda.time.Duration;

public class JodaTimeDurationExample7 {
    public static void main(String[] args) {
        Duration duration = Duration.standardMinutes(1);

        System.out.println(duration.toString());
    }
}
The output is:
PT60S

Create Duration object from specified number of seconds

In the following Java program, we use the Duration.standardSeconds() static method to create Duration object from specified number of seconds.

JodaTimeDurationExample8.java

import org.joda.time.Duration;

public class JodaTimeDurationExample8 {
    public static void main(String[] args) {
        Duration duration = Duration.standardSeconds(1);

        System.out.println(duration.toString());
    }
}
The output is:
PT1S

Create Duration object from specified number of milliseconds

In the following Java program, we learn how to use the Duration.millis() static method to create Duration object from specified number of milliseconds.

JodaTimeDurationExample9.java

import org.joda.time.Duration;

public class JodaTimeDurationExample9 {
    public static void main(String[] args) {
        Duration duration = Duration.millis(1000);

        System.out.println(duration.toString());
    }
}
The output is:
PT1S

Get Duration value as number of Days Hours Minutes or Seconds

JodaTimeDurationExample10.java

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

public class JodaTimeDurationExample10 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime threeDaysAgo = now.minusDays(3);

        // Create three days duration
        Duration duration = new Duration(threeDaysAgo, now);
        long days = duration.getStandardDays();
        long hours = duration.getStandardHours();
        long minutes = duration.getStandardMinutes();
        long seconds = duration.getStandardSeconds();
        long milliseconds = duration.getMillis();

        System.out.println("Duration: " + duration);
        System.out.println(days + " days");
        System.out.println(hours + " hours");
        System.out.println(minutes+ " minutes");
        System.out.println(seconds + " seconds");
        System.out.println(milliseconds+ " milliseconds");
    }
}
The output is:
Duration: PT259200S
3 days
72 hours
4320 minutes
259200 seconds
259200000 milliseconds

Convert Duration to Joda-Time Days Hours Minutes Seconds

JodaTimeDurationExample11.java

import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;

public class JodaTimeDurationExample11 {
    public static void main(String[] args) {
        Duration fiveDaysDuration = Duration.standardDays(5);

        Days days = fiveDaysDuration.toStandardDays();
        Hours hours = fiveDaysDuration.toStandardHours();
        Minutes minutes = fiveDaysDuration.toStandardMinutes();
        Seconds seconds = fiveDaysDuration.toStandardSeconds();

        System.out.println(days);
        System.out.println(hours);
        System.out.println(minutes);
        System.out.println(seconds);
    }
}
The output is:
P5D
PT120H
PT7200M
PT432000S

How to use Duration.withMillis()

The following Java program is using the Duration.withMillis() method to create a new Duration instance with a different milliseconds value.

JodaTimeDurationExample12.java

import org.joda.time.Duration;

public class JodaTimeDurationExample12 {
    public static void main(String[] args) {
        Duration twoSecondDuration = Duration.standardSeconds(2);

        Duration oneSecondDuration = twoSecondDuration.withMillis(1000);

        System.out.println(oneSecondDuration);
        System.out.println(twoSecondDuration);
    }
}
The output is:
PT1S
PT2S

How to use Duration.withDurationAdded()

JodaTimeDurationExample13.java

import org.joda.time.Duration;

public class JodaTimeDurationExample13 {
    public static void main(String[] args) {
        Duration oneSecondDuration = Duration.standardSeconds(1);

        Duration threeSecondDuration = oneSecondDuration.withDurationAdded(1000, 2);

        System.out.println(oneSecondDuration);
        System.out.println(threeSecondDuration);
    }
}
The output is:
PT1S
PT3S

How to use Duration.plus()

JodaTimeDurationExample14.java

import org.joda.time.Duration;

public class JodaTimeDurationExample14 {
    public static void main(String[] args) {
        Duration oneSecondDuration = Duration.standardSeconds(1);

        Duration twoSecondDuration = oneSecondDuration.plus(1000); // 1000 milliseconds = 1 seconds

        System.out.println(oneSecondDuration);
        System.out.println(twoSecondDuration);
    }
}
The output is:
PT1S
PT2S

How to use Duration.minus()

JodaTimeDurationExample15.java

import org.joda.time.Duration;

public class JodaTimeDurationExample15 {
    public static void main(String[] args) {
        Duration twoSecondDuration = Duration.standardSeconds(2);

        Duration oneSecondDuration = twoSecondDuration.minus(1000); // 1000 milliseconds = 1 seconds

        System.out.println(oneSecondDuration);
        System.out.println(twoSecondDuration);
    }
}
The output is:
PT1S
PT2S

How to use Duration.multipliedBy()

JodaTimeDurationExample16.java

import org.joda.time.Duration;

public class JodaTimeDurationExample16 {
    public static void main(String[] args) {
        Duration oneSecondDuration = Duration.standardSeconds(1);

        Duration fiveSecondsDuration = oneSecondDuration.multipliedBy(5);

        System.out.println(oneSecondDuration);
        System.out.println(fiveSecondsDuration);
    }
}
The output is:
PT1S
PT5S

How to use Duration.dividedBy()

JodaTimeDurationExample17.java

import org.joda.time.Duration;

public class JodaTimeDurationExample17 {
    public static void main(String[] args) {
        Duration tenSecondsDuration = Duration.standardSeconds(10);

        Duration twoSecondsDuration = tenSecondsDuration.dividedBy(5);

        System.out.println(twoSecondsDuration);
        System.out.println(tenSecondsDuration);
    }
}
The output is:
PT2S
PT10S

How to use Duration.negated()

JodaTimeDurationExample18.java

import org.joda.time.Duration;

public class JodaTimeDurationExample18 {
    public static void main(String[] args) {
        Duration duration1 = Duration.standardSeconds(10);

        Duration duration2 = duration1.negated();

        System.out.println(duration1);
        System.out.println(duration2);
    }
}
The output is:
PT10S
PT-10S

How to use Duration.abs()

JodaTimeDurationExample19.java

import org.joda.time.Duration;

public class JodaTimeDurationExample19 {
    public static void main(String[] args) {
        Duration duration1 = Duration.standardSeconds(-5);

        Duration duration2 = duration1.abs();

        System.out.println(duration1);
        System.out.println(duration2);
    }
}
The output is:
PT-5S
PT5S

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 Period by Examples in Java

Joda Time Interval by Examples in Java