JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

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:

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:

  • 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.

Sections in This Chapter

java.util.DateFormat - Formatting Date Objects to Strings

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

Dr. Herong Yang, updated in 2008
java.util.SimpleDateFormat.parse() - Parsing Date Strings to Objects