JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
java.util.SimpleDateFormat.parse() - Parsing Date Strings to Objects
This section provides a tutorial example on how to use the java.util.SimpleDateFormat.parse() method to parse or convert date and time strings to date objects.
DateFormat objects can also be used to convert date strings back to Date objects using the parse() method, see the following demonstration program:
/* DateParsingTest.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.util.*; import java.text.*; class DateParsingTest { public static void main(String[] a) { parseDate(); parseDateSpecial(); } public static void parseDate() { SimpleDateFormat mf = new SimpleDateFormat("MMM-yyyy"); SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss.SSS"); SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); SimpleDateFormat dtf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS"); String ms = "Oct-2002"; String ts = "22:13:19.123"; String ds = "21-Oct-2002"; String dts = "21-Oct-2002 22:13:19.123"; try { System.out.println("Parsing " +ms +" with " +mf.toPattern()); System.out.println(" " + mf.parse(ms)); System.out.println("Parsing " +ts +" with " +tf.toPattern()); System.out.println(" " + tf.parse(ts)); System.out.println("Parsing " +ds +" with " +df.toPattern()); System.out.println(" " + df.parse(ds)); System.out.println("Parsing " + dts + " with "+ dtf.toPattern()); System.out.println(" " + dtf.parse(dts)); System.out.println("Parsing " +dts +" with "+df.toPattern()); System.out.println(" " + df.parse(dts)); System.out.println("Parsing " +ds +" with "+dtf.toPattern()); System.out.println(" " + dtf.parse(ds)); } catch (ParseException e) { System.out.println("Exception: " + e.toString()); } } public static void parseDateSpecial() { SimpleDateFormat dtf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); String str0 = "Mon Oct 21 20:54:05 EDT 2002"; // correct String str1 = "Mon Oct 20 20:54:05 EDT 2002"; // wrong d.o.w. String str2 = "Mon Oct 21 25:54:05 EDT 2002"; // wrong hour String str3 = "Mon Oct 21 20:54:05 PDT 2002"; // time zone String str4 = "Mon Oct 1 20:54:05 EDT 2002"; // missing a 'd' try { System.out.println("Parsing " + str0 + " with " + dtf.toPattern()); System.out.println(" " + dtf.parse(str0)); System.out.println("Parsing " + str1 + " with " + dtf.toPattern()); System.out.println(" " + dtf.parse(str1)); System.out.println("Parsing " + str2 + " with " + dtf.toPattern()); System.out.println(" " + dtf.parse(str2)); System.out.println("Parsing " + str3 + " with " + dtf.toPattern()); System.out.println(" " + dtf.parse(str3)); System.out.println("Parsing " + str4 + " with " + dtf.toPattern()); System.out.println(" " + dtf.parse(str4)); } catch (ParseException e) { System.out.println("Exception: " + e.toString()); } } }
Output:
Parsing Oct-2002 with MMM-yyyy Tue Oct 01 00:00:00 EDT 2002 Parsing 22:13:19.123 with HH:mm:ss.SSS Thu Jan 01 22:13:19 EST 1970 Parsing 21-Oct-2002 with dd-MMM-yyyy Mon Oct 21 00:00:00 EDT 2002 Parsing 21-Oct-2002 22:13:19.123 with dd-MMM-yyyy HH:mm:ss.SSS Mon Oct 21 22:13:19 EDT 2002 Parsing 21-Oct-2002 22:13:19.123 with dd-MMM-yyyy Mon Oct 21 00:00:00 EDT 2002 Parsing 21-Oct-2002 with dd-MMM-yyyy HH:mm:ss.SSS Exception: java.text.ParseException: Unparseable date: "21-Oct-2002" Parsing Mon Oct 21 20:54:05 EDT 2002 with EEE MMM dd HH:mm:ss zzz yyyy Mon Oct 21 20:54:05 EDT 2002 Parsing Mon Oct 20 20:54:05 EDT 2002 with EEE MMM dd HH:mm:ss zzz yyyy Sun Oct 20 20:54:05 EDT 2002 Parsing Mon Oct 21 25:54:05 EDT 2002 with EEE MMM dd HH:mm:ss zzz yyyy Tue Oct 22 01:54:05 EDT 2002 Parsing Mon Oct 21 20:54:05 PDT 2002 with EEE MMM dd HH:mm:ss zzz yyyy Mon Oct 21 23:54:05 EDT 2002 Parsing Mon Oct 1 20:54:05 EDT 2002 with EEE MMM dd HH:mm:ss zzz yyyy Tue Oct 01 20:54:05 EDT 2002
Examining the output carefully, you will see each parsing example shows a different feature of the parse() method:
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