By dave | March 22, 2015

There are several ways to format dates in Java, but by far the easiest is to use DateFormat. Creating a DateFormat is very similar to NumberFormat that we saw on the previous page. Here are the static factory methods called directly on the DateFormat class:

  • getDateInstance(..)
  • getDateTimeInstance(..)
  • getTimeInstance(..)

There are several overloaded versions of each method above. We will not cover Locale here as that will be covered in another article on java timezone support. However, there is another parameter called style, which allows customization of how the date or time is displayed. See the DateFormat object for a full list. Common styles are SHORT, MEDIUM and LONG.

import java.text.DateFormat;
import java.util.Date;

public class Formatting
{
    public static void main(String[] args)
    {
        // get the current time
        Date date = new Date();

        // create a formatter that outputs a short date and time.
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                                       DateFormat.SHORT);
        String val = df.format(date);

        System.out.println(val);
    }
}

Output:

30/11/07 22:57

Using SimpleDateFormat to further customize output

If you need more control over what gets output then you will probably end up using SimpleDateFormat. This class provides much more detailed means of specifying how to format a date. A full and complete list of all options is beyond this guide, but can be found online in the SimpleDateFormat javadocs.

Unlike previous examples where we used a factory method to create the formatting object, in this case we call new directly to get a new instance, normally passing the required format string as the parameter. This class uses similar formatting codes to String.format explained earlier in this article.

import java.text.SimpleDateFormat;
import java.util.Date;

public class Formatting
{
    public static void main(String[] args)
    {
        // get the current time
        Date date = new Date();

        // outputs in day/mon/year eg 02/DEC/2007
        SimpleDateFormat df1 = new SimpleDateFormat("dd/MMM/yyyy");
        System.out.println(df1.format(date));

        // outputs in day month, year time eg 02 December 2007 11:40
        SimpleDateFormat df2 = new SimpleDateFormat("dd MMMMMMM, yyyy. hh:mm");
        System.out.println(df2.format(date));
    }
}

Output

30/Nov/2007
30 November, 2007. 10:58

I hope this has given you insight into formatting various types into Strings, again this article only gets you started with the various classes, there's a lot of extra detail not covered in here.

Other pages within this category

comments powered by Disqus

This site uses cookies to analyse traffic, and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.

Send a message
X

Please use the forum for help with UI & libraries.

This message will be securely transmitted to our servers.