JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
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) 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 Feb 19 17:37:51 ART 2024 Default DateFormat: 2/19/24, 5:37 PM Default DateFormat in west coast: 2/19/24, 12:37 PM Short date time in US: 2/19/24, 5:37 PM Medium date time in US: Feb 19, 2024, 5:37:51 PM Short date time in French: 19/02/2024 17:37 Long date time in French: 19 février 2024, 17:37:51 ART Date toString() format: Mon Feb 19 17:37:51 ART 2024 Default SimpleDateFormat: 2/19/24, 5:37 PM The FDA standard format: 19-Feb-2024 Date.toString() format: Mon Feb 19 17:37:51 ART 2024 Date.toString() format in French: lun. févr. 19 17:37:51 ART 2024
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.
Table of Contents
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
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
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 - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs