Java SimpleDateFormat example: How to convert a Date to a formatted String

Summary: This tutorial demonstrates how to use the Java SimpleDateFormat class to convert a Java Date to a formatted String.

The Java SimpleDateFormat class provides lets you easily (a) convert between a Java String to a Date, or (b) perform the opposite conversion, from a Date to a String. In this article I'll at several different ways to convert a Java Date object to a nicely formatted String object, and in another article I'll look at a couple of nice ways to convert in the opposite direction.

A first Java SimpleDateFormat example

In the first example, I start by getting today's date as a java.util.Date object, and then create a SimpleDateFormat object to define the format I want for the date after it's converted to a String.

Here's the source code for a complete Java SimpleDateFormat example class that demonstrates this custom date formatting approach:

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

/**
 * SimpleDateFormat example: Convert from a Date to a formatted String
 *
 * Get today's date,
 * then convert it to a String, 
 * using the date format we specify.
 */
public class JavaSimpleDateFormatTest
{
  public static void main(String[] args)
  {
    // (1) get today's date
    Date today = Calendar.getInstance().getTime();

    // (2) create a date "formatter" (the date format we want)
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");

    // (3) create a new String using the date format we want
    String folderName = formatter.format(today);
    
    // (4) this prints "Folder Name = 2009-09-06-08.23.23"
    System.out.println("Folder Name = " + folderName);
  }
}

As you can see from the comments in the code, the custom date format (or pattern) I specified looked like this:

yyyy-MM-dd-hh.mm.ss

and the resulting formatted date output looks like this:

2009-09-06-08.23.23

This Java source code was taken from an actual program, where I append this formatted date string to the end of a filename, so I can easily look at the filename and tell the date and time each file was created.

SimpleDateFormat: Many other custom date formats

As you might guess, you can format a date in many, many different ways. Here are some of the most common custom date formats I've used:

yyyy-MM-dd                 results in    2009-09-06
yyyyMMdd                   results in    20090906
EEE MMM dd hh:mm:ss yyyy   results in    Sun Sep 06 08:32:51 2009

Java SimpleDateFormat: Easily get the date, time, or datetime

On a related note, if you just need to quickly get the date or time in a nice format, the SimpleDateFormat class also offers several nice static convenience methods that make it easy to get (a) the date, (b) the time, or (c) both date and time in just a line or two of Java code.

Here’s the complete source code for another class that demonstrates these static methods of the Java SimpleDateFormat class:

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class JavaSimpleDateFormatTest2
{
  public static void main(String[] args)
  {

    // prints "Sep 6, 2009"
    DateFormat dateInstance = SimpleDateFormat.getDateInstance();
    System.out.println(dateInstance.format(Calendar.getInstance().getTime()));

    // prints "9:03:20 PM"
    DateFormat timeInstance = SimpleDateFormat.getTimeInstance();
    System.out.println(timeInstance.format(Calendar.getInstance().getTime()));
  
    // prints "Sep 6, 2009 9:03:20 PM"
    DateFormat dateTimeInstance = SimpleDateFormat.getDateTimeInstance();
    System.out.println(dateTimeInstance.format(Calendar.getInstance().getTime()));

  }
}

Java SimpleDateFormat formatting options

As you might guess, there are a wealth of Java date formatting options you can use with the Java SimpleDateFormat class to create a formatted String from a given Date instance. These options are all shown on the SimpleDateFormat javadoc page.

The opposite direction: Use SimpleDateFormat to convert from String to Date

If you need to convert in the opposite direction, from a Java String to a Date, here's a link to my Convert a String to a Date using SimpleDateFormat tutorial.