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

20211029
20211029T091631.884+0800
20211029T091631+0800
2021302
2021W435
2021W435T091631.884+0800

Maven Dependencies

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.7</version>
</dependency>

Maven Central

Wayan

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.