How do I format date and time using DateFormatUtils class?

Category: commons.lang, viewed: 9377 time(s).

The DateFormatUtils class help us to format date and time information. This class use an instance of FastDateFormat class to format the date and time information. Compared to Java SimpleDateFormat, the FastDateFormat class is thread safe.

If you want to create a custom date format you can use the FastDateFormat class directly.

package org.kodejava.example.commons.lang;

import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;

public class DateFormatting {
    public static void main(String[] args) {
        Date today = new Date();
        
        /*
         * ISO8601 formatter for date-time without time zone.
         * The format used is yyyy-MM-dd'T'HH:mm:ss.
         */
        String timestamp = DateFormatUtils.ISO_DATETIME_FORMAT.format(today);
        System.out.println("timestamp = " + timestamp);

        /*
         * ISO8601 formatter for date-time with time zone.
         * The format used is yyyy-MM-dd'T'HH:mm:ssZZ.
         */
        timestamp = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(today);
        System.out.println("timestamp = " + timestamp);        
        
        /*
         * The format used is <tt>EEE, dd MMM yyyy HH:mm:ss Z</tt> in US locale.
         */
        timestamp = DateFormatUtils.SMTP_DATETIME_FORMAT.format(today);
        System.out.println("timestamp = " + timestamp);        
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats