java.util.DateFormat - Formatting Date Objects to Strings

This section provides a tutorial example on how to use java.util.DateFormat and java.util.SimpleDateFormat classes to convert or format data objects into text strings in a specific calendar in various locales.

In the previous chapter, we learned how to use java.util.Date, java.util.Calendar and related classes to manage date, time and calendar information as objects or numeric values. If you want to convert or format date, time and calendar information into text strings in different human languages and styles, you need to learn 2 more JDK classes.

The DateFormat class, java.text.DateFormat, is an abstract class providing a foundation for subclasses to present date objects in a specific calendar in text strings, and parsing text string back to date objects.

The SimpleDateFormat class, java.text.SimpleDateFormat, is concrete subclass of the DateFormat class. It can be customized with different formatting patterns and in different locales.

Once a DateFormat object is created with a predefined pattern or a customized pattern, it can be used to convert a Date object into a string based on the pattern using the format() method. The following program shows how to create DateFormat objects and how to use them:

/* DateFormatTest.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
import java.text.*;
class DateFormatTest {
   public static void main(String[] a) {
      showDateFormat();
      showSimpleDateFormat();
   }
   public static void showDateFormat() {
      Date now = new Date(); // the current time
      System.out.println("Date toString() format: " + now.toString());
      DateFormat df = DateFormat.getInstance();
      System.out.println("Default DateFormat: " + df.format(now));
      df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
      System.out.println("Default DateFormat in west coast: " 
         + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
         DateFormat.SHORT, Locale.US);
      System.out.println("Short date time in US: " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
         DateFormat.MEDIUM, Locale.US);
      System.out.println("Medium date time in US: " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
         DateFormat.SHORT, Locale.FRENCH);
      System.out.println("Short date time in French: " 
         + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.FRENCH);
      System.out.println("Long date time in French: "+df.format(now));
   }
   public static void showSimpleDateFormat() {
      Date now = new Date(); // the current time
      System.out.println("Date toString() format: " + now.toString());
      SimpleDateFormat sdf = new SimpleDateFormat();
      System.out.println("Default SimpleDateFormat: "
         + sdf.format(now));
      sdf = new SimpleDateFormat("dd-MMM-yyyy");
      System.out.println("The FDA standard format: "+sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
      System.out.println("Date.toString() format: " +sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
         Locale.FRENCH);
      System.out.println("Date.toString() format in French: "
         + sdf.format(now));
   }
}

Output:

Date toString() format: Mon Oct 21 20:54:05 EDT 2002
Default DateFormat: 10/21/02 8:54 PM
Default DateFormat in west coast: 10/21/02 5:54 PM
Short date time in US: 10/21/02 8:54 PM
Medium date time in US: Oct 21, 2002 8:54:05 PM
Short date time in French: 21/10/02 20:54
Long date time in French: 21 octobre 2002 20:54:05 EDT
Date toString() format: Mon Oct 21 20:54:05 EDT 2002
Default SimpleDateFormat: 10/21/02 8:54 PM
The FDA standard format: 21-Oct-2002
Date.toString() format: Mon Oct 21 20:54:05 EDT 2002
Date.toString() format in French: lun. oct. 21 20:54:05 EDT 2002

Note that the DateFormat class is providing predefined formats for a number of commonly used styles in different locales. The predefined formats are accessible by the get*Instance() methods.

Note also that the DateFormat class uses GregorianCalendar as the default calendar.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

Date and Time Object and String Conversion

java.util.DateFormat - Formatting Date Objects to Strings

 java.util.SimpleDateFormat.parse() - Parsing Date Strings to Objects

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version