Joda Time Period by Examples in Java

Tags: Joda Time Joda Period Joda DateTime Joda Weeks Joda Days Joda Hours Joda Minutes Joda Seconds Joda Duration Joda DurationFieldType

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

The Period class is an immutable time period class that specifies a set of duration field values.

Create Period object using constructors

You can create a new empty Period using the default constructor as below Java program.

JodaTimePeriodExample1.java

import org.joda.time.Period;

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

        Period period = new Period();

        System.out.println("New empty Period: " + period);
    }
}
The output is:
New empty Period: PT0S

You can create a new Period object with given time information as the following Java program.

JodaTimePeriodExample2.java

import org.joda.time.Period;

public class JodaTimePeriodExample2 {
    public static void main(String[] args) {
        int hours = 1;
        int minutes = 20;
        int seconds = 30;
        int milliseconds = 400;

        Period period = new Period(hours, minutes, seconds, milliseconds);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT1H20M30.400S

You can create a new Period object with a number of years, months, days and time as following Java program.

JodaTimePeriodExample3.java

import org.joda.time.Period;

public class JodaTimePeriodExample3 {
    public static void main(String[] args) {
        int years = 1;
        int months = 1;
        int weeks = 1;
        int days = 10;
        int hours = 1;
        int minutes = 20;
        int seconds = 30;
        int milliseconds = 400;

        Period period = new Period(years, months, weeks, days, hours, minutes, seconds, milliseconds);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P1Y1M1W10DT1H20M30.400S

You can create a new Period object with a given number of milliseconds. JodaTimePeriodExample4

import org.joda.time.Period;

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

        Period period = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minutes

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT1M

You can create a new Period object with given start and end time in milliseconds. JodaTimePeriodExample5.java

import org.joda.time.Period;

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

        Period period = new Period(1000, 9000);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT8S

You can create a new Period object with a given interval start end date time in DateTime type. JodaTimePeriodExample6.java

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

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

        Period period = new Period(now, tomorrow);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P1D

Parse String into Period

JodaTimePeriodExample7.java

import org.joda.time.Period;

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

        Period period1 = Period.parse("P1Y1M1W10DT1H20M30.400S"); // 1 year, 1 month, 1 week, 10 days, 1 hour, 20 minutes, 30 seconds, 400 milliseconds
        Period period2 = Period.parse("P1DT10M30.000S");// 1 day, 10 minutes, 30 seconds

        System.out.println("New Period 1: " + period1);
        System.out.println("New Period 2: " + period2);
    }
}
The output is:
New Period 1: P1Y1M1W10DT1H20M30.400S
New Period 2: P1DT10M30S

Create Period object with a specified number of years

You can create a new Period object using Period.years() static method with a given number of years.

JodaTimePeriodExample8.java

import org.joda.time.Period;

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

        Period period = Period.years(5);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P5Y

Create Period object with a specified number of months

You can create a new Period object using Period.months() static method with a given number of months.

JodaTimePeriodExample9.java

import org.joda.time.Period;

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

        Period period = Period.months(12);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P12M

Create Period object with a specified number of weeks

You can create a new Period object using Period.weeks() static method with a given number of weeks.

JodaTimePeriodExample10.java

import org.joda.time.Period;

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

        Period period = Period.weeks(2);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P2W

Create Period object with a specified number of days

You can create a new Period object using Period.days() static method with a given number of days.

JodaTimePeriodExample11.java

import org.joda.time.Period;

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

        Period period = Period.days(5);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: P5D

Create Period object with a specified number of hours

You can create a new Period object using Period.hours() static method with a given number of hours.

JodaTimePeriodExample12.java

import org.joda.time.Period;

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

        Period period = Period.hours(23);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT23H

Create Period object with a specified number of minutes

You can create a new Period object using Period.minutes() static method with a given number of minutes.

JodaTimePeriodExample13.java

import org.joda.time.Period;

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

        Period period = Period.minutes(200);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT200M

Create Period object with a specified number of seconds

You can create a new Period object using Period.seconds() static method with a given number of seconds.

JodaTimePeriodExample14.java

import org.joda.time.Period;

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

        Period period = Period.seconds(90);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT90S

Create Period object with a specified number of milliseconds

You can create a new Period object using Period.millis() static method with a given number of milliseconds.

JodaTimePeriodExample15.java

import org.joda.time.Period;

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

        Period period = Period.millis(999);

        System.out.println("New Period: " + period);
    }
}
The output is:
New Period: PT0.999S

Get specified values of Period object

The Period class provides getter methods to get specified values as following Java program.

JodaTimePeriodExample16.java

import org.joda.time.Period;

public class JodaTimePeriodExample16 {
    public static void main(String[] args) {
        Period period = new Period(1, 2, 3, 4, 5, 6, 7, 8);

        int years = period.getYears();
        int months = period.getMonths();
        int weeks = period.getWeeks();
        int days = period.getDays();
        int hours = period.getHours();
        int minutes = period.getMinutes();
        int seconds = period.getSeconds();
        int milliseconds = period.getMillis();

        System.out.println("Period: " + period);
        System.out.println("Years: " + years);
        System.out.println("Months: " + months);
        System.out.println("Weeks: " + weeks);
        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:
Period: P1Y2M3W4DT5H6M7.008S
Years: 1
Months: 2
Weeks: 3
Days: 4
Hours: 5
Minutes: 6
Seconds: 7
Milliseconds: 8

Convert Period to Joda-Time Weeks

JodaTimePeriodExample17.java

import org.joda.time.Period;
import org.joda.time.Weeks;

public class JodaTimePeriodExample17 {
    public static void main(String[] args) {
        Period period = Period.weeks(4);

        Weeks weeks = period.toStandardWeeks();

        System.out.println("Period: " + period);
        System.out.println("Weeks: " + weeks);
    }
}
The output is:
Period: P4W
Weeks: P4W

Convert Period to Joda-Time Days

JodaTimePeriodExample18.java

import org.joda.time.Days;
import org.joda.time.Period;

public class JodaTimePeriodExample18 {
    public static void main(String[] args) {
        Period period = Period.weeks(4);

        Days days = period.toStandardDays();

        System.out.println("Period: " + period);
        System.out.println("Days: " + days);
    }
}
The output is:
Period: P4W
Days: P28D

Convert Period to Joda-Time Hours

JodaTimePeriodExample19.java

import org.joda.time.Hours;
import org.joda.time.Period;

public class JodaTimePeriodExample19 {
    public static void main(String[] args) {
        Period period = Period.days(1);

        Hours hours = period.toStandardHours();

        System.out.println("Period: " + period);
        System.out.println("Hours: " + hours);
    }
}
The output is:
Period: P1D
Hours: PT24H

Convert Period to Joda-Time Minutes

JodaTimePeriodExample20.java

import org.joda.time.Minutes;
import org.joda.time.Period;

public class JodaTimePeriodExample20 {
    public static void main(String[] args) {
        Period period = Period.hours(1);

        Minutes minutes = period.toStandardMinutes();

        System.out.println("Period: " + period);
        System.out.println("Minutes: " + minutes);
    }
}
The output is:
Period: PT1H
Minutes: PT60M

Convert Period to Joda-Time Seconds

JodaTimePeriodExample21.java

import org.joda.time.Period;
import org.joda.time.Seconds;

public class JodaTimePeriodExample21 {
    public static void main(String[] args) {
        Period period = Period.minutes(1);

        Seconds seconds = period.toStandardSeconds();

        System.out.println("Period: " + period);
        System.out.println("Seconds: " + seconds);
    }
}
The output is:
Period: PT1M
Seconds: PT60S

Convert Period to Joda-Time Duration

JodaTimePeriodExample22.java

import org.joda.time.Duration;
import org.joda.time.Period;

public class JodaTimePeriodExample22 {
    public static void main(String[] args) {
        Period period = Period.minutes(1);

        Duration duration = period.toStandardDuration();

        System.out.println("Period: " + period);
        System.out.println("Duration: " + duration);
    }
}
The output is:
Period: PT1M
Duration: PT60S

How to use Period.withField() method

JodaTimePeriodExample23.java

import org.joda.time.DurationFieldType;
import org.joda.time.Period;

public class JodaTimePeriodExample23 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(1);

        Period period2 = period1.withField(DurationFieldType.days(), 10);
        Period period3 = period1.withField(DurationFieldType.minutes(), 2);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
        System.out.println("Period 3: " + period3);
    }
}
The output is:
Period 1: PT1M
Period 2: P10DT1M
Period 3: PT2M

How to use Period.withFieldAdded() method

JodaTimePeriodExample24.java

import org.joda.time.DurationFieldType;
import org.joda.time.Period;

public class JodaTimePeriodExample24 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(1);

        Period period2 = period1.withFieldAdded(DurationFieldType.minutes(), 10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT1M
Period 2: PT11M

How to use Period.withYears() method

JodaTimePeriodExample25.java

import org.joda.time.Period;

public class JodaTimePeriodExample25 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(1);

        Period period2 = period1.withYears(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT1M
Period 2: P10YT1M

How to use Period.withMonths() method

JodaTimePeriodExample26.java

import org.joda.time.Period;

public class JodaTimePeriodExample26 {
    public static void main(String[] args) {
        Period period1 = Period.months(1);

        Period period2 = period1.withMonths(5);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P1M
Period 2: P5M

How to use Period.withWeeks() method

JodaTimePeriodExample27.java

import org.joda.time.Period;

public class JodaTimePeriodExample27 {
    public static void main(String[] args) {
        Period period1 = Period.weeks(4);

        Period period2 = period1.withWeeks(8);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P4W
Period 2: P8W

How to use Period.withDays() method

JodaTimePeriodExample28.java

import org.joda.time.Period;

public class JodaTimePeriodExample28 {
    public static void main(String[] args) {
        Period period1 = Period.days(10);

        Period period2 = period1.withDays(20);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P10D
Period 2: P20D

How to use Period.withHours() method

JodaTimePeriodExample29.java

import org.joda.time.Period;

public class JodaTimePeriodExample29 {
    public static void main(String[] args) {
        Period period1 = Period.hours(5);

        Period period2 = period1.withHours(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT5H
Period 2: PT10H

How to use Period.withMinutes() method

JodaTimePeriodExample30.java

import org.joda.time.Period;

public class JodaTimePeriodExample30 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(90);

        Period period2 = period1.withMinutes(100);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT90M
Period 2: PT100M

How to use Period.withSeconds() method

JodaTimePeriodExample31.java

import org.joda.time.Period;

public class JodaTimePeriodExample31 {
    public static void main(String[] args) {
        Period period1 = Period.seconds(20);

        Period period2 = period1.withSeconds(50);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT20S
Period 2: PT50S

How to use Period.withMillis() method

JodaTimePeriodExample32.java

import org.joda.time.Period;

public class JodaTimePeriodExample32 {
    public static void main(String[] args) {
        Period period1 = Period.millis(500);

        Period period2 = period1.withMillis(999);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT0.500S
Period 2: PT0.999S

How to use Period.plus() method

JodaTimePeriodExample33.java

import org.joda.time.Period;

public class JodaTimePeriodExample33 {
    public static void main(String[] args) {
        Period period1 = Period.millis(500);
        Period period2 = Period.millis(500);

        Period period3 = period1.plus(period2);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
        System.out.println("Period 3: " + period3);
    }
}
The output is:
Period 1: PT0.500S
Period 2: PT0.500S
Period 3: PT1S

How to use Period.plusYears() method

JodaTimePeriodExample34.java

import org.joda.time.Period;

public class JodaTimePeriodExample34 {
    public static void main(String[] args) {
        Period period1 = Period.months(2);

        Period period2 = period1.plusYears(1);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P2M
Period 2: P1Y2M

How to use Period.plusMonths() method

JodaTimePeriodExample35.java

import org.joda.time.Period;

public class JodaTimePeriodExample35 {
    public static void main(String[] args) {
        Period period1 = Period.months(2);

        Period period2 = period1.plusMonths(1);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P2M
Period 2: P3M

How to use Period.plusWeeks() method

JodaTimePeriodExample36.java

import org.joda.time.Period;

public class JodaTimePeriodExample36 {
    public static void main(String[] args) {
        Period period1 = Period.months(2);

        Period period2 = period1.plusWeeks(1);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P2M
Period 2: P2M1W

How to use Period.plusDays() method

JodaTimePeriodExample37.java

import org.joda.time.Period;

public class JodaTimePeriodExample37 {
    public static void main(String[] args) {
        Period period1 = Period.days(10);

        Period period2 = period1.plusDays(5);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P10D
Period 2: P15D

How to use Period.plusHours() method

JodaTimePeriodExample38.java

import org.joda.time.Period;

public class JodaTimePeriodExample38 {
    public static void main(String[] args) {
        Period period1 = Period.hours(40);

        Period period2 = period1.plusHours(5);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT40H
Period 2: PT45H

How to use Period.plusMinutes() method

JodaTimePeriodExample39.java

import org.joda.time.Period;

public class JodaTimePeriodExample39 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(30);

        Period period2 = period1.plusMinutes(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT30M
Period 2: PT40M

How to use Period.plusSeconds() method

JodaTimePeriodExample40.java

import org.joda.time.Period;

public class JodaTimePeriodExample40 {
    public static void main(String[] args) {
        Period period1 = Period.seconds(30);

        Period period2 = period1.plusSeconds(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT30S
Period 2: PT40S

How to use Period.plusMillis() method

JodaTimePeriodExample41.java

import org.joda.time.Period;

public class JodaTimePeriodExample41 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(500);

        Period period2 = period1.plusMillis(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT500M
Period 2: PT500M0.010S

How to use Period.minus() method

JodaTimePeriodExample42.java

import org.joda.time.Period;

public class JodaTimePeriodExample42 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(500);
        Period period2 = Period.minutes(300);

        Period period3 = period1.minus(period2);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
        System.out.println("Period 3: " + period3);
    }
}
The output is:
Period 1: PT500M
Period 2: PT300M
Period 3: PT200M

How to use Period.minusYears() method

JodaTimePeriodExample43.java

import org.joda.time.Period;

public class JodaTimePeriodExample43 {
    public static void main(String[] args) {
        Period period1 = Period.years(5);

        Period period2 = period1.minusYears(3);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P5Y
Period 2: P2Y

How to use Period.minusMonths() method

JodaTimePeriodExample44.java

import org.joda.time.Period;

public class JodaTimePeriodExample44 {
    public static void main(String[] args) {
        Period period1 = Period.months(10);

        Period period2 = period1.minusMonths(4);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P10M
Period 2: P6M

How to use Period.minusWeeks() method

JodaTimePeriodExample45.java

import org.joda.time.Period;

public class JodaTimePeriodExample45 {
    public static void main(String[] args) {
        Period period1 = Period.weeks(7);

        Period period2 = period1.minusWeeks(4);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P7W
Period 2: P3W

How to use Period.minusDays() method

JodaTimePeriodExample46.java

import org.joda.time.Period;

public class JodaTimePeriodExample46 {
    public static void main(String[] args) {
        Period period1 = Period.days(7);

        Period period2 = period1.minusDays(4);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: P7D
Period 2: P3D

How to use Period.minusHours() method

JodaTimePeriodExample47.java

import org.joda.time.Period;

public class JodaTimePeriodExample47 {
    public static void main(String[] args) {
        Period period1 = Period.hours(24);

        Period period2 = period1.minusHours(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT24H
Period 2: PT14H

How to use Period.minusMinutes() method

JodaTimePeriodExample48.java

import org.joda.time.Period;

public class JodaTimePeriodExample48 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(24);

        Period period2 = period1.minusMinutes(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT24M
Period 2: PT14M

How to use Period.minusSeconds() method

JodaTimePeriodExample49.java

import org.joda.time.Period;

public class JodaTimePeriodExample49 {
    public static void main(String[] args) {
        Period period1 = Period.seconds(90);

        Period period2 = period1.minusSeconds(10);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT90S
Period 2: PT80S

How to use Period.minusMillis() method

JodaTimePeriodExample50.java

import org.joda.time.Period;

public class JodaTimePeriodExample50 {
    public static void main(String[] args) {
        Period period1 = Period.millis(900);

        Period period2 = period1.minusMillis(200);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT0.900S
Period 2: PT0.700S

How to use Period.multipliedBy() method

JodaTimePeriodExample51.java

import org.joda.time.Period;

public class JodaTimePeriodExample51 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(7);

        Period period2 = period1.multipliedBy(2);

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT7M
Period 2: PT14M

How to use Period.negated() method

JodaTimePeriodExample52.java

import org.joda.time.Period;

public class JodaTimePeriodExample52 {
    public static void main(String[] args) {
        Period period1 = Period.minutes(7);

        Period period2 = period1.negated();

        System.out.println("Period 1: " + period1);
        System.out.println("Period 2: " + period2);
    }
}
The output is:
Period 1: PT7M
Period 2: PT-7M

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