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

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!!

  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");
    
  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.

Leave a Reply