Joda Time DateTime by Examples in Java

Tags: Joda Time DateTime DateTimeZone Instant LocalDateTime LocalDate LocalTime DateTimeFieldType DurationFieldType Period Duration Chronology GregorianChronology EthiopicChronology Joda DateTime Joda DateTimeZone Joda Instant Joda LocalDateTime Joda LocalDate Joda LocalTime Joda DateTimeFieldType Joda DurationFieldType Joda Period Joda Duration Joda Chronology Joda GregorianChronology Joda EthiopicChronology

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

How to create DateTime object using constructors

JodaTimeDateTimeExample1.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample1 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();

        DateTime dateTime1 = new DateTime();
        DateTime dateTime2 = new DateTime(dateTimeZone);
        DateTime dateTime3 = new DateTime(chronology);

        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
    }
}
The output is:
2021-01-17T13:21:25.746+07:00
2021-01-17T06:21:25.747Z
2021-01-17T13:21:25.747+07:00

JodaTimeDateTimeExample2.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample2 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();
        int milliseconds = 100000;

        DateTime dateTime1 = new DateTime(milliseconds);
        DateTime dateTime2 = new DateTime(milliseconds, dateTimeZone);
        DateTime dateTime3 = new DateTime(milliseconds, chronology);

        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
    }
}
The output is:
1970-01-01T07:01:40.000+07:00
1970-01-01T01:01:40.000+01:00
1970-01-01T07:01:40.000+07:00

JodaTimeDateTimeExample3.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample3 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();
        Instant instant = new Instant();

        DateTime dateTime1 = new DateTime(instant);
        DateTime dateTime2 = new DateTime(instant, dateTimeZone);
        DateTime dateTime3 = new DateTime(instant, chronology);

        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
    }
}
The output is:
2021-01-17T06:21:53.311Z
2021-01-17T06:21:53.311Z
2021-01-17T13:21:53.311+07:00

JodaTimeDateTimeExample4.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample4 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();
        int year = 2021;
        int monthOfYear = 1;
        int dayOfMonth = 10;
        int hourOfDay = 20;
        int minuteOfHour = 30;
        int secondOfMinute = 40;
        int millisOfSecond = 500;

        DateTime dateTime1 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour);
        DateTime dateTime2 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, dateTimeZone);
        DateTime dateTime3 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, chronology);

        DateTime dateTime4 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute);
        DateTime dateTime5 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, dateTimeZone);
        DateTime dateTime6 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, chronology);

        DateTime dateTime7 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
        DateTime dateTime8 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, dateTimeZone);
        DateTime dateTime9 = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology);

        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
        System.out.println(dateTime4);
        System.out.println(dateTime5);
        System.out.println(dateTime6);
        System.out.println(dateTime7);
        System.out.println(dateTime8);
        System.out.println(dateTime9);
    }
}
The output is:
2021-01-10T20:30:00.000+07:00
2021-01-10T20:30:00.000Z
2021-01-10T20:30:00.000+07:00
2021-01-10T20:30:40.000+07:00
2021-01-10T20:30:40.000Z
2021-01-10T20:30:40.000+07:00
2021-01-10T20:30:40.500+07:00
2021-01-10T20:30:40.500Z
2021-01-10T20:30:40.500+07:00

How to use DateTime.now()

JodaTimeDateTimeExample5.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample5 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();

        DateTime dateTime1 = DateTime.now();
        DateTime dateTime2 = DateTime.now(dateTimeZone);
        DateTime dateTime3 = DateTime.now(chronology);

        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
    }
}
The output is:
2021-01-17T13:23:11.943+07:00
2021-01-17T06:23:11.943Z
2021-01-17T13:23:11.943+07:00

Parse a String to DateTime object

JodaTimeDateTimeExample6.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample6 {
    public static void main(String[] args) {
        DateTime dateTime = DateTime.parse("2021-01-17T10:44:35.205+07:00");

        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T10:44:35.205+07:00

How to use toDateTimeISO() method

This method returns the DateTime object that uses ISOChronology in the default zone.

JodaTimeDateTimeExample7.java

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

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

        DateTime dateTime = new DateTime(chronology);

        DateTime dateTimeIso = dateTime.toDateTimeISO();

        System.out.println(dateTime);
        System.out.println(dateTimeIso);
    }
}
The output is:
2013-05-09T13:23:38.515+07:00
2021-01-17T13:23:38.515+07:00

How to use toDateTime() method

JodaTimeDateTimeExample8.java

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeDateTimeExample8 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();

        DateTime now = DateTime.now();

        DateTime dateTime1 = now.toDateTime(dateTimeZone);
        DateTime dateTime2 = now.toDateTime(chronology);

        System.out.println(now);
        System.out.println(dateTime1);
        System.out.println(dateTime2);
    }
}
The output is:
2021-01-17T13:23:56.626+07:00
2021-01-17T06:23:56.626Z
2021-01-17T13:23:56.626+07:00

Convert DateTime to LocalDateTime

The method DateTime.toLocalDateTime() to convert a DateTime object to LocalDateTime object as following Java program.

JodaTimeDateTimeExample9.java

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;

public class JodaTimeDateTimeExample9 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        LocalDateTime localDateTime = now.toLocalDateTime();

        System.out.println(now);
        System.out.println(localDateTime);
    }
}
The output is:
2021-01-17T13:24:10.547+07:00
2021-01-17T13:24:10.547

Convert DateTime to LocalDate

In the following Java program, we use DateTime.toLocalDate() method to convert a DateTime object to LocalDate object.

JodaTimeDateTimeExample10.java

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

public class JodaTimeDateTimeExample10 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        LocalDate localDate = now.toLocalDate();

        System.out.println(now);
        System.out.println(localDate);
    }
}
The output is:
2021-01-17T13:24:22.684+07:00
2021-01-17

Convert DateTime to LocalTime

In the following Java program, we use DateTime.toLocalTime() method to convert a DateTime object to LocalTime object.

JodaTimeDateTimeExample11.java

import org.joda.time.DateTime;
import org.joda.time.LocalTime;

public class JodaTimeDateTimeExample11 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        LocalTime localTime = now.toLocalTime();

        System.out.println(now);
        System.out.println(localTime);
    }
}
The output is:
2021-01-17T13:25:48.619+07:00
13:25:48.619

How to get specific field of a DateTime object

JodaTimeDateTimeExample53.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;

public class JodaTimeDateTimeExample53 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        int year = now.get(DateTimeFieldType.year());
        int monthOfYear = now.get(DateTimeFieldType.monthOfYear());
        int dayOfMonth = now.get(DateTimeFieldType.dayOfMonth());
        int hourOfDay = now.get(DateTimeFieldType.hourOfDay());
        int minuteOfHour = now.get(DateTimeFieldType.minuteOfHour());
        int secondOfMinute = now.get(DateTimeFieldType.secondOfMinute());
        int millisOfSecond = now.get(DateTimeFieldType.millisOfSecond());

        System.out.println(now);
        System.out.println(year);
        System.out.println(monthOfYear);
        System.out.println(dayOfMonth);
        System.out.println(hourOfDay);
        System.out.println(minuteOfHour);
        System.out.println(secondOfMinute);
        System.out.println(millisOfSecond);
    }
}
The output is:
2021-01-17T13:26:05.555+07:00
2021
1
17
13
26
5
555

JodaTimeDateTimeExample54.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample54 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        int year = now.getYear();
        int monthOfYear = now.getMonthOfYear();
        int dayOfMonth = now.getDayOfMonth();
        int hourOfDay = now.getHourOfDay();
        int minuteOfHour = now.getMinuteOfHour();
        int secondOfMinute = now.getSecondOfMinute();
        int millisOfSecond = now.getMillisOfSecond();

        System.out.println(now);
        System.out.println(year);
        System.out.println(monthOfYear);
        System.out.println(dayOfMonth);
        System.out.println(hourOfDay);
        System.out.println(minuteOfHour);
        System.out.println(secondOfMinute);
        System.out.println(millisOfSecond);
    }
}
The output is:
2021-01-17T13:26:20.158+07:00
2021
1
17
13
26
20
158

How to use DateTime.withYear() method

JodaTimeDateTimeExample12.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample12 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime dateTime = now.withYear(1999);

        System.out.println(now);
        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T13:26:38.349+07:00
1999-01-17T13:26:38.349+07:00

How to use DateTime.withWeekyear() method

JodaTimeDateTimeExample13.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample13 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime dateTime = now.withWeekyear(2);

        System.out.println(now);
        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T13:26:53.806+07:00
0002-01-13T13:26:53.806+06:42:04

How to use DateTime.withMonthOfYear() method

JodaTimeDateTimeExample14.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample14 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime julyMonth = now.withMonthOfYear(7);

        System.out.println(now);
        System.out.println(julyMonth);
    }
}
The output is:
2021-01-17T13:27:02.864+07:00
2021-07-17T13:27:02.864+07:00

How to use DateTime.withWeekOfWeekyear() method

JodaTimeDateTimeExample15.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample15 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime thirdWeekOfYear = now.withDayOfWeek(3);

        System.out.println(now);
        System.out.println(thirdWeekOfYear);
    }
}
The output is:
2021-01-17T13:27:19.672+07:00
2021-01-13T13:27:19.672+07:00

How to use DateTime.withDayOfYear() method

JodaTimeDateTimeExample16.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample16 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime day100OfTheYear = now.withDayOfYear(100);

        System.out.println(now);
        System.out.println(day100OfTheYear);
    }
}
The output is:
2021-01-17T13:27:34.683+07:00
2021-04-10T13:27:34.683+07:00

How to use DateTime.withDayOfMonth() method

JodaTimeDateTimeExample17.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample17 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime secondDayOfMonth = now.withDayOfMonth(2);

        System.out.println(now);
        System.out.println(secondDayOfMonth);
    }
}
The output is:
2021-01-17T13:27:47.046+07:00
2021-01-02T13:27:47.046+07:00

How to use DateTime.withDayOfWeek() method

JodaTimeDateTimeExample18.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample18 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime mondayThisWeek = now.withDayOfWeek(1);

        System.out.println(now);
        System.out.println(mondayThisWeek);
    }
}
The output is:
2021-01-17T13:28:03.866+07:00
2021-01-11T13:28:03.866+07:00

How to use DateTime.withHourOfDay() method

JodaTimeDateTimeExample19.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample19 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime at7Today = now.withHourOfDay(7);

        System.out.println(now);
        System.out.println(at7Today);
    }
}
The output is:
2021-01-17T13:28:18.800+07:00
2021-01-17T07:28:18.800+07:00

How to use DateTime.withMinuteOfHour() method

JodaTimeDateTimeExample20.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample20 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime at30Minutes = now.withMinuteOfHour(30);

        System.out.println(now);
        System.out.println(at30Minutes);
    }
}
The output is:
2021-01-17T13:28:33.148+07:00
2021-01-17T13:30:33.148+07:00

How to use DateTime.withSecondOfMinute() method

JodaTimeDateTimeExample21.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample21 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime at10Seconds = now.withSecondOfMinute(10);

        System.out.println(now);
        System.out.println(at10Seconds);
    }
}
The output is:
2021-01-17T13:28:47.254+07:00
2021-01-17T13:28:10.254+07:00

How to use DateTime.withMillisOfSecond() method

JodaTimeDateTimeExample22.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample22 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime at500Milliseconds = now.withMillisOfSecond(500);

        System.out.println(now);
        System.out.println(at500Milliseconds);
    }
}
The output is:
2021-01-17T13:45:12.245+07:00
2021-01-17T13:45:12.500+07:00

How to use DateTime.withMillisOfDay() method

JodaTimeDateTimeExample23.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample23 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime firstMinuteOfToday = now.withMillisOfDay(60000);

        System.out.println(now);
        System.out.println(firstMinuteOfToday);
    }
}
The output is:
2021-01-17T13:45:28.371+07:00
2021-01-17T00:01:00.000+07:00

How to use DateTime.withMillis() method

JodaTimeDateTimeExample24.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample24 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime firstMinuteFrom1970 = now.withMillis(60000);

        System.out.println(now);
        System.out.println(firstMinuteFrom1970);
    }
}
The output is:
2021-01-17T13:45:39.917+07:00
1970-01-01T07:01:00.000+07:00

How to use DateTime.withChronology() method

JodaTimeDateTimeExample25.java

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

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

        DateTime dateTime = now.withChronology(chronology);

        System.out.println(now);
        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T13:45:53.520+07:00
2013-05-09T13:45:53.520+07:00

How to use DateTime.withZone() method

JodaTimeDateTimeExample26.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class JodaTimeDateTimeExample26 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        DateTime now = DateTime.now();

        DateTime dateTime = now.withZone(dateTimeZone);

        System.out.println(now);
        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T13:46:05.671+07:00
2021-01-17T06:46:05.671Z

How to use DateTime.withZoneRetainFields() method

JodaTimeDateTimeExample27.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class JodaTimeDateTimeExample27 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        DateTime now = DateTime.now();

        DateTime dateTime = now.withZoneRetainFields(dateTimeZone);

        System.out.println(now);
        System.out.println(dateTime);
    }
}
The output is:
2021-01-17T13:46:17.817+07:00
2021-01-17T13:46:17.817Z

How to use DateTime.withDate() method

JodaTimeDateTimeExample28.java

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

public class JodaTimeDateTimeExample28 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        LocalDate localDate = new LocalDate(2021, 1, 1);

        DateTime dateTime1 = now.withDate(localDate);

        DateTime dateTime2 = now.withDate(1999, 2, 2);


        System.out.println(now);
        System.out.println(dateTime1);
        System.out.println(dateTime2);
    }
}
The output is:
2021-01-17T13:55:18.043+07:00
2021-01-01T13:55:18.043+07:00
1999-02-02T13:55:18.043+07:00

How to use DateTime.withTime() method

JodaTimeDateTimeExample29.java

import org.joda.time.DateTime;
import org.joda.time.LocalTime;

public class JodaTimeDateTimeExample29 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        LocalTime localTime = new LocalTime(10, 20, 30);

        DateTime dateTime1 = now.withTime(localTime);

        DateTime dateTime2 = now.withTime(5, 10, 15, 20);
        
        System.out.println(now);
        System.out.println(dateTime1);
        System.out.println(dateTime2);
    }
}
The output is:
2021-01-17T13:55:38.561+07:00
2021-01-17T10:20:30.000+07:00
2021-01-17T05:10:15.020+07:00

How to use DateTime.withTimeAtStartOfDay() method

JodaTimeDateTimeExample30.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample30 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime atStartOfDay = now.withTimeAtStartOfDay();

        System.out.println(now);
        System.out.println(atStartOfDay);
    }
}
The output is:
2021-01-17T13:55:52.347+07:00
2021-01-17T00:00:00.000+07:00

How to use DateTime.withField() method

JodaTimeDateTimeExample31.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;

public class JodaTimeDateTimeExample31 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime dateTime1 = now.withField(DateTimeFieldType.year(), 1999);
        DateTime dateTime2 = now.withField(DateTimeFieldType.monthOfYear(), 7);
        DateTime dateTime3 = now.withField(DateTimeFieldType.dayOfMonth(), 10);
        DateTime dateTime4 = now.withField(DateTimeFieldType.hourOfDay(), 9);
        DateTime dateTime5 = now.withField(DateTimeFieldType.minuteOfHour(), 20);
        DateTime dateTime6 = now.withField(DateTimeFieldType.secondOfMinute(), 30);
        DateTime dateTime7 = now.withField(DateTimeFieldType.millisOfSecond(), 999);

        System.out.println(now);
        System.out.println(dateTime1);
        System.out.println(dateTime2);
        System.out.println(dateTime3);
        System.out.println(dateTime4);
        System.out.println(dateTime5);
        System.out.println(dateTime6);
        System.out.println(dateTime7);
    }
}
The output is:
2021-01-17T13:56:11.087+07:00
1999-01-17T13:56:11.087+07:00
2021-07-17T13:56:11.087+07:00
2021-01-10T13:56:11.087+07:00
2021-01-17T09:56:11.087+07:00
2021-01-17T13:20:11.087+07:00
2021-01-17T13:56:30.087+07:00
2021-01-17T13:56:11.999+07:00

How to use DateTime.withFieldAdded() method

JodaTimeDateTimeExample32.java

import org.joda.time.DateTime;
import org.joda.time.DurationFieldType;

public class JodaTimeDateTimeExample32 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextYear = now.withFieldAdded(DurationFieldType.years(), 1);
        DateTime nextMonth = now.withFieldAdded(DurationFieldType.months(), 1);
        DateTime next5Days = now.withFieldAdded(DurationFieldType.days(), 5);
        DateTime next1Hour = now.withFieldAdded(DurationFieldType.hours(), 1);
        DateTime next5Minutes = now.withFieldAdded(DurationFieldType.minutes(), 5);
        DateTime next30Seconds = now.withFieldAdded(DurationFieldType.seconds(), 30);
        DateTime next500Milliseconds = now.withFieldAdded(DurationFieldType.millis(), 500);

        System.out.println(now);
        System.out.println(nextYear);
        System.out.println(nextMonth);
        System.out.println(next5Days);
        System.out.println(next1Hour);
        System.out.println(next5Minutes);
        System.out.println(next30Seconds);
        System.out.println(next500Milliseconds);
    }
}
The output is:
2021-01-17T13:56:26.723+07:00
2022-01-17T13:56:26.723+07:00
2021-02-17T13:56:26.723+07:00
2021-01-22T13:56:26.723+07:00
2021-01-17T14:56:26.723+07:00
2021-01-17T14:01:26.723+07:00
2021-01-17T13:56:56.723+07:00
2021-01-17T13:56:27.223+07:00

How to use DateTime.withDurationAdded() method

JodaTimeDateTimeExample33.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample33 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime next2Seconds = now.withDurationAdded(1000, 2);

        System.out.println(now);
        System.out.println(next2Seconds);
    }
}
The output is:
2021-01-17T13:56:42.165+07:00
2021-01-17T13:56:44.165+07:00

How to use DateTime.withPeriodAdded() method

JodaTimeDateTimeExample34.java

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

public class JodaTimeDateTimeExample34 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        Period period = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute

        DateTime next2Minutes = now.withPeriodAdded(period, 2);

        System.out.println(now);
        System.out.println(next2Minutes);
    }
}
The output is:
2021-01-17T13:56:55.258+07:00
2021-01-17T13:58:55.258+07:00

How to use DateTime.plus() method

JodaTimeDateTimeExample35.java

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

public class JodaTimeDateTimeExample35 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        Period period = new Period(120000); // 120000 milliseconds = 120 seconds = 2 minutes
        Duration duration = new Duration(180000); // 180000 milliseconds = 180 seconds = 3 minutes

        DateTime next1Minute = now.plus(60000); // 60000 milliseconds = 60 seconds = 1 minute

        DateTime next2Minutes = now.plus(period);

        DateTime next3Minutes = now.plus(duration);

        System.out.println(now);
        System.out.println(next1Minute);
        System.out.println(next2Minutes);
        System.out.println(next3Minutes);
    }
}
The output is:
2021-01-17T13:58:30.271+07:00
2021-01-17T13:59:30.271+07:00
2021-01-17T14:00:30.271+07:00
2021-01-17T14:01:30.271+07:00

How to use DateTime.plusYears() method

JodaTimeDateTimeExample36.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample36 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextFiveYear = now.plusYears(5);

        System.out.println(now);
        System.out.println(nextFiveYear);
    }
}
The output is:
2021-01-17T13:58:43.879+07:00
2026-01-17T13:58:43.879+07:00

How to use DateTime.plusMonths() method

JodaTimeDateTimeExample37.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample37 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextTwoMonths = now.plusMonths(2);

        System.out.println(now);
        System.out.println(nextTwoMonths);
    }
}
The output is:
2021-01-17T13:58:57.405+07:00
2021-03-17T13:58:57.405+07:00

How to use DateTime.plusWeeks() method

JodaTimeDateTimeExample38.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample38 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextTwoWeeks = now.plusWeeks(2);

        System.out.println(now);
        System.out.println(nextTwoWeeks);
    }
}
The output is:
2021-01-17T13:59:10.732+07:00
2021-01-31T13:59:10.732+07:00

How to use DateTime.plusDays() method

JodaTimeDateTimeExample39.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample39 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextTenDays = now.plusDays(10);

        System.out.println(now);
        System.out.println(nextTenDays);
    }
}
The output is:
2021-01-17T13:59:22.809+07:00
2021-01-27T13:59:22.809+07:00

How to use DateTime.plusHours() method

JodaTimeDateTimeExample40.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample40 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextHour = now.plusHours(1);

        System.out.println(now);
        System.out.println(nextHour);
    }
}
The output is:
2021-01-17T13:59:36.164+07:00
2021-01-17T14:59:36.164+07:00

How to use DateTime.plusMinutes() method

JodaTimeDateTimeExample41.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample41 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime next30Minutes = now.plusMinutes(30);

        System.out.println(now);
        System.out.println(next30Minutes);
    }
}
The output is:
2021-01-17T13:59:48.285+07:00
2021-01-17T14:29:48.285+07:00

How to use DateTime.plusSeconds() method

JodaTimeDateTimeExample42.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample42 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime next30Seconds = now.plusSeconds(30);

        System.out.println(now);
        System.out.println(next30Seconds);
    }
}
The output is:
2021-01-17T14:00:00.108+07:00
2021-01-17T14:00:30.108+07:00

How to use DateTime.plusMillis() method

JodaTimeDateTimeExample43.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample43 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime nextSecond = now.plusMillis(1000);

        System.out.println(now);
        System.out.println(nextSecond);
    }
}
The output is:
2021-01-17T14:00:13.587+07:00
2021-01-17T14:00:14.587+07:00

How to use DateTime.minus() method

JodaTimeDateTimeExample44.java

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

public class JodaTimeDateTimeExample44 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();
        Period twoMinutesPeriod = new Period(120000); // 120000 milliseconds = 120 seconds = 2 minutes
        Duration threeMinutesDuration = new Duration(180000); // 180000 milliseconds = 180 seconds = 3 minutes

        DateTime oneMinuteAgo = now.minus(60000); // 60000 milliseconds = 60 seconds = 1 minute
        DateTime twoMinutesAgo = now.minus(twoMinutesPeriod);
        DateTime threeMinutesAgo = now.minus(threeMinutesDuration);

        System.out.println(now);
        System.out.println(oneMinuteAgo);
        System.out.println(twoMinutesAgo);
        System.out.println(threeMinutesAgo);
    }
}
The output is:
2021-01-17T14:00:26.736+07:00
2021-01-17T13:59:26.736+07:00
2021-01-17T13:58:26.736+07:00
2021-01-17T13:57:26.736+07:00

How to use DateTime.minusYears() method

JodaTimeDateTimeExample45.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample45 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime oneYearAgo = now.minusYears(1);

        System.out.println(now);
        System.out.println(oneYearAgo);
    }
}
The output is:
2021-01-17T14:00:41.406+07:00
2020-01-17T14:00:41.406+07:00

How to use DateTime.minusMonths() method

JodaTimeDateTimeExample46.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample46 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime oneMonthAgo = now.minusMonths(1);

        System.out.println(now);
        System.out.println(oneMonthAgo);
    }
}
The output is:
2021-01-17T14:00:53.093+07:00
2020-12-17T14:00:53.093+07:00

How to use DateTime.minusWeeks() method

JodaTimeDateTimeExample47.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample47 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime twoWeeksAgo = now.minusWeeks(2);

        System.out.println(now);
        System.out.println(twoWeeksAgo);
    }
}
The output is:
2021-01-17T14:01:05.196+07:00
2021-01-03T14:01:05.196+07:00

How to use DateTime.minusDays() method

JodaTimeDateTimeExample48.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample48 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime oneDayAgo = now.minusDays(1);

        System.out.println(now);
        System.out.println(oneDayAgo);
    }
}
The output is:
2021-01-17T14:01:56.490+07:00
2021-01-16T14:01:56.490+07:00

How to use DateTime.minusHours() method

JodaTimeDateTimeExample49.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample49 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime threeHoursAgo = now.minusHours(3);

        System.out.println(now);
        System.out.println(threeHoursAgo);
    }
}
The output is:
2021-01-17T14:02:09.221+07:00
2021-01-17T11:02:09.221+07:00

How to use DateTime.minusMinutes() method

JodaTimeDateTimeExample50.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample50 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime fiveMinutesAgo = now.minusMinutes(5);

        System.out.println(now);
        System.out.println(fiveMinutesAgo);
    }
}
The output is:
2021-01-17T14:02:22.328+07:00
2021-01-17T13:57:22.328+07:00

How to use DateTime.minusSeconds() method

JodaTimeDateTimeExample51.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample51 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime tenSecondsAgo = now.minusSeconds(10);

        System.out.println(now);
        System.out.println(tenSecondsAgo);
    }
}
The output is:
2021-01-17T14:02:34.040+07:00
2021-01-17T14:02:24.040+07:00

How to use DateTime.minusMillis() method

JodaTimeDateTimeExample52.java

import org.joda.time.DateTime;

public class JodaTimeDateTimeExample52 {
    public static void main(String[] args) {
        DateTime now = DateTime.now();

        DateTime oneSecondAgo = now.minusMillis(1000);

        System.out.println(now);
        System.out.println(oneSecondAgo);
    }
}
The output is:
2021-01-17T14:02:47.469+07:00
2021-01-17T14:02:46.469+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

Convert Joda-Time DateTime into Date in Java

Convert SQL Timestamp into Joda-Time DateTime and Vice Versa in Java

Convert Joda-Time DateTime into String and Vice Versa in Java

Convert Joda-Time DateTime into Calendar and Vice Versa in Java

Compare Joda-Time DateTime using DateTimeComparator in Java

Joda-Time Create DateTime with Time End of the Day in Java

Joda Time Duration by Examples in Java

Get Date and Time fields value of Joda-Time DateTime object in Java

Joda Time Period by Examples in Java

Joda Time Interval by Examples in Java