How do I format date information in Joda?

The example show you how to format the string representation of a date. In Joda we can use the DateTime‘s class toString() method. The method accept the pattern of the date format and the locale information.

package org.kodejava.example.joda;

import org.joda.time.DateTime;

import java.util.Locale;

public class FormattingDemo {
    //
    // Define the date format pattern.
    //
    private static final String pattern = "E MM/dd/yyyy HH:mm:ss.SSS";
    
    public static void main(String[] args) {
        //
        // Creates a new instance of DateTime object.
        //
        DateTime dateTime = new DateTime();

        //
        // Print out the date as a formatted string using the defined
        // Locale information.
        //
        System.out.println(dateTime.toString(pattern));
        System.out.println(dateTime.toString(pattern, Locale.GERMANY));
        System.out.println(dateTime.toString(pattern, Locale.FRENCH));
        System.out.println(dateTime.toString(pattern, Locale.JAPANESE));
    }
}

Here an the example result of our program:

Fri 02/03/2012 17:21:14.480
Fr 02/03/2012 17:21:14.480
ven. 02/03/2012 17:21:14.480
金 02/03/2012 17:21:14.480
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

Leave a Reply