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);
}
}New empty Period: PT0SYou 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);
}
}New Period: PT1H20M30.400SYou 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);
}
}New Period: P1Y1M1W10DT1H20M30.400SYou 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);
}
}New Period: PT1MYou 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);
}
}New Period: PT8SYou 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);
}
}New Period: P1DParse 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);
}
}New Period 1: P1Y1M1W10DT1H20M30.400S
New Period 2: P1DT10M30SCreate 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);
}
}New Period: P5YCreate 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);
}
}New Period: P12MCreate 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);
}
}New Period: P2WCreate 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);
}
}New Period: P5DCreate 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);
}
}New Period: PT23HCreate 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);
}
}New Period: PT200MCreate 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);
}
}New Period: PT90SCreate 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);
}
}New Period: PT0.999SGet 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);
}
}Period: P1Y2M3W4DT5H6M7.008S
Years: 1
Months: 2
Weeks: 3
Days: 4
Hours: 5
Minutes: 6
Seconds: 7
Milliseconds: 8Convert 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);
}
}Period: P4W
Weeks: P4WConvert 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);
}
}Period: P4W
Days: P28DConvert 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);
}
}Period: P1D
Hours: PT24HConvert 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);
}
}Period: PT1H
Minutes: PT60MConvert 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);
}
}Period: PT1M
Seconds: PT60SConvert 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);
}
}Period: PT1M
Duration: PT60SHow 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);
}
}Period 1: PT1M
Period 2: P10DT1M
Period 3: PT2MHow 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);
}
}Period 1: PT1M
Period 2: PT11MHow 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);
}
}Period 1: PT1M
Period 2: P10YT1MHow 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);
}
}Period 1: P1M
Period 2: P5MHow 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);
}
}Period 1: P4W
Period 2: P8WHow 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);
}
}Period 1: P10D
Period 2: P20DHow 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);
}
}Period 1: PT5H
Period 2: PT10HHow 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);
}
}Period 1: PT90M
Period 2: PT100MHow 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);
}
}Period 1: PT20S
Period 2: PT50SHow 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);
}
}Period 1: PT0.500S
Period 2: PT0.999SHow 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);
}
}Period 1: PT0.500S
Period 2: PT0.500S
Period 3: PT1SHow 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);
}
}Period 1: P2M
Period 2: P1Y2MHow 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);
}
}Period 1: P2M
Period 2: P3MHow 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);
}
}Period 1: P2M
Period 2: P2M1WHow 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);
}
}Period 1: P10D
Period 2: P15DHow 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);
}
}Period 1: PT40H
Period 2: PT45HHow 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);
}
}Period 1: PT30M
Period 2: PT40MHow 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);
}
}Period 1: PT30S
Period 2: PT40SHow 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);
}
}Period 1: PT500M
Period 2: PT500M0.010SHow 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);
}
}Period 1: PT500M
Period 2: PT300M
Period 3: PT200MHow 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);
}
}Period 1: P5Y
Period 2: P2YHow 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);
}
}Period 1: P10M
Period 2: P6MHow 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);
}
}Period 1: P7W
Period 2: P3WHow 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);
}
}Period 1: P7D
Period 2: P3DHow 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);
}
}Period 1: PT24H
Period 2: PT14HHow 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);
}
}Period 1: PT24M
Period 2: PT14MHow 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);
}
}Period 1: PT90S
Period 2: PT80SHow 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);
}
}Period 1: PT0.900S
Period 2: PT0.700SHow 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);
}
}Period 1: PT7M
Period 2: PT14MHow 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);
}
}Period 1: PT7M
Period 2: PT-7MHappy Coding 😊
Related Articles
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