How to add hours, minutes, seconds into DateTime in Joda Time?

In this example you’ll learn how to add some hours, minutes or seconds into the DateTime object in Joda Time. Some couple methods available to plus or minus some hours, minutes or seconds from the object, as you can see in the example below.

The DateTime object is an immutable object, which means calling one of the plus or minus method does not modified the current object. Instead, these method return a new DateTime object for each method calls.

package org.kodejava.example.joda;

import org.joda.time.DateTime;

public class TimeCalculationDemo {
    public static void main(String[] args) {
        //
        // Creates an instance of current DateTime which represents the
        // current date time.
        //
        DateTime dateTime = new DateTime();
        System.out.println("DateTime = " + dateTime);

        //
        // Plus some hours, minutes, and seconds to the original DateTime.
        //
        System.out.println("Plus 1 hour is " + dateTime.plusHours(1));
        System.out.println("Plus 10 minutes is " + dateTime.plusMinutes(10));
        System.out.println("Plus 60 seconds is " + dateTime.plusSeconds(60));

        //
        // Minus some hours, minutes, and seconds to the original DateTime.
        //
        System.out.println("Minus 1 hour is " + dateTime.minusHours(1));
        System.out.println("Minus 10 minutes is " + dateTime.minusMinutes(10));
        System.out.println("Minus 60 seconds is " + dateTime.minusSeconds(60));
    }
}

The program print the following result:

DateTime = 2012-07-27T12:08:46.048+08:00
Plus 1 hour is 2012-07-27T13:08:46.048+08:00
Plus 10 minutes is 2012-07-27T12:18:46.048+08:00
Plus 60 seconds is 2012-07-27T12:09:46.048+08:00
Minus 1 hour is 2012-07-27T11:08:46.048+08:00
Minus 10 minutes is 2012-07-27T11:58:46.048+08:00
Minus 60 seconds is 2012-07-27T12:07:46.048+08:00
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

2 Comments

  1. It not working as expected. Here is my code and result.

    package com.demo;
    
    import org.joda.time.DateTime;
    
    public class App {
        public static void main(String[] args) {
            DateTime dateTime=new DateTime();
            System.out.println("Date Time :"+dateTime);
            dateTime.plusHours(1);
            System.out.println("Date Time :"+dateTime);
            dateTime.plusMinutes(4);
            System.out.println("Date Time :"+dateTime);
        }
    }
    

    Result:
    Date Time :2016-08-29T03:31:39.047+07:00
    Date Time :2016-08-29T03:31:39.047+07:00
    Date Time :2016-08-29T03:31:39.047+07:00

  2. Hello Hien Khuu,

    Joda-Tme objects are immutable. When you change the property of a DateTime object it will not change the current object, instead it will return a new copy. You need to assign the modification result into a new object to get the modified value.

    For example:

    DateTime plusOneHour = dateTime.plusHours(1);
    DateTime plusFourMinute = plusOneHour.plusMinutes(4);
    

    Or you could combine the operation:

    DateTime plusOneHourFourMinute = dateTime.plusHours(1).plusMinutes(4);
    

Leave a Reply