JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Formatting and Parsing Dates

Part:   1  2  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

Parsing Strings for Dates

DateFormat objects can also be used to convert string back to Date objects using the parse() method, see the following demonstration program:

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 changed
      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

Examing the output carefully, you will see each parsing example shows a different feature of the parse() method:

  • If a field is missing, it will be set to its lowermost value.
  • If the year field is missing, it will be set to 1970.
  • If time fields are missing, they will be set to 0.
  • If time zone is missing, the local time zone will be used.
  • If the input string is longer than the pattern, the remaining part of the string will be ignored.
  • If the input string is not matching the pattern, you will get an exception.
  • Long pattern is not a problem.
  • Day of month overrides day of week.
  • An overflow lower field will change the higher field. Hour 25 gives hour 1 plus 1 day.
  • Specified time zone overrides local time zone.
  • Single digit day of month matches double digits pattern.

Part:   1  2  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Formatting and Parsing Dates