Hassle-free date formatting in Java

July 2nd, 2007

Are you a Java programmer and always hated it to handle Dates, Calendars, DateFormatter and the kind just to display a simple and small java.util.Date correctly?

One again the Apache commons library comes to a rescue! Programmer’s little helper in this lesson is the FastDateFormat class. Just instantiate a n object , pass your desired Locale and you are ready to format any Date with date and/or time to your wishes. Either you use the built-in formattes “SHORT, MEDIUM, LONG, FULL” or pass a custom pattern.

Here’s a little example:

import org.apache.commons.lang.time.FastDateFormat;
FastDateFormat df = FastDateFormat.getDateTimeInstance(
FastDateFormat.SHORT, FastDateFormat.SHORT, Locale.US);
System.out.writeln(df.format(creationdate));

FastDateFormat has a lot of initializers and functions to format your date and time values for your needs. Make sure to check out more classes of the org.apache.commons.lang package, that holds a lot of gems which make a java programmer’s day much easier.

Leave a Reply