How do I create DateTime object in Joda-Time?

The following example show you a various way to create an instance of Joda-Time’s DateTime class. By using the default constructor we will create an object with the current system date time. We can also create the object by passing the information like year, month, day, hour, minutes and second.

Joda can also use an instance from JDK’s java.util.Date and java.util.Calendar to create the DateTime. This means that the date object of JDK and Joda can be used to work together in our application.

package org.kodejava.example.joda;

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

import java.util.Calendar;
import java.util.Date;

public class DateTimeDemo {

    public static void main(String[] args) {
        // Creates DateTime object using the default constructor will
        // give you the current system date.
        DateTime date = new DateTime();
        System.out.println("date = " + date);

        // Or simply calling the now() method.
        date =;
        System.out.println("date = " + date);

        // Creates DateTime object with information like year, month,
        // day, hour, minute, second and milliseconds
        date = new DateTime(2017, 1, 15, 0, 0, 0, 0);
        System.out.println("date = " + date);

        // Create DateTime object from milliseconds.
        date = new DateTime(System.currentTimeMillis());
        System.out.println("date = " + date);

        // Create DateTime object from Date object.
        date = new DateTime(new Date());
        System.out.println("date = " + date);

        // Create DateTime object from Calendar object.
        Calendar calendar = Calendar.getInstance();
        date = new DateTime(calendar);
        System.out.println("date = " + date);

        // Create DateTime object from string. The format of the
        // string  should be precise.
        date = new DateTime("2017-01-15T13:14:00.000+08:00");
        System.out.println("date = " + date);
        date = DateTime.parse("2017-01-15");
        System.out.println("date = " + date);
        date = DateTime.parse("15/01/2017", DateTimeFormat.forPattern("dd/MM/yyyy"));
        System.out.println("date = " + date);

The result of our code snippet:

date = 2017-01-15T13:15:39.248+08:00
date = 2017-01-15T13:15:39.395+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T13:15:39.405+08:00
date = 2017-01-15T13:15:39.406+08:00
date = 2017-01-15T13:15:39.424+08:00
date = 2017-01-15T13:14:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00

Maven Dependencies

<!-- -->

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply