How do I format date in Joda-Time using ISODateTimeFormat class?

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

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 = DateTime.now();

        // 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:

20170116
20170116T233404.650+0800
20170116T233404+0800
2017016
2017W031
2017W031T233404.650+0800

Maven Dependencies

<!-- http://repo1.maven.org/maven2/joda-time/joda-time/2.9.7/joda-time-2.9.7.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.7</version>
</dependency>

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

3 Comments

  1. Hi,

    I am trying to use JODA time to set format (MMddyyyy hh:mm a) , but it turns out it treats a 4 digit year as 2 digit. I get an error while parsing, IllegalArgumentException as 2013/9/8 formed as 13/9/8.

    What will be the solution to it?

    Thanks!!

    Reply
  2. Hi,

    Were you trying to convert a string to a DateTime? If yes, you can use the org.joda.time.format.DateTimeFormatter. Here is an example:

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd");
    DateTime datetime = formatter.parseDateTime("2013/09/08");
    
    Reply
  3. Hi, In my spring with MongoDB project I want to fetch the data between two ISODate. How it is possible? Please help me. The data in the MongoDB is like following:

    Collection name is mycollection and there is a field name creationTime like this:

    "creationTime" : { 
        "logtime" : ISODate("2013-09-12T08:39:07.227Z"), 
        "logtimeStr" : "12-09-2013 02:09:07", 
        "day" : 12, 
        "month" : 9, 
        "year" : 2013, 
        "hour" : 14, 
        "min" : 9, 
        "second" : 7 
    }
    

    and now I want to retrieve data from this collection on between two logtime by using Spring.

    Reply

Leave a Reply