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

How do I format date in Joda using ISODateTimeFormat class?

This example demonstrate how to use the ISODateTimeFormat class to format the date time information in Joda.

package org.kodejava.example.joda;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

public class ISODateTimeFormatDemo {
    public static void main(String[] args) {
        DateTime dateTime = new DateTime();

        //
        // Returns a basic formatter for a full date as four digit
        // year, two digit month of year, and two digit day of
        // month yyyyMMdd.
        //
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicDate()));
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicDateTime()));
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicDateTimeNoMillis()));

        //
        // Returns a formatter for a full ordinal date, using a four
        // digit year and three digit dayOfYear yyyyDDD.
        //
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicOrdinalDate()));

        //
        // Returns a basic formatter for a full date as four digit
        // weekyear, two digit week of weekyear, and one digit day
        // of week xxxx'W'wwe
        //
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicWeekDate()));
        System.out.println(dateTime.toString(
                ISODateTimeFormat.basicWeekDateTime()));
    }
}

The result of the code above is printed below:

20120228
20120228T163810.037+0800
20120228T163810+0800
2012059
2012W092
2012W092T163810.037+0800