Date parsing in Java

Updated

Parsing time and date in Java can be a bit rough experience when you’re doing it for the first time. Fortunately it’s not that hard when you get the hang of it.

Java language contains an abstract DateFormat class which is created for manipulating dates. Since the DateFormat is abstract, you need to use a subclass, namely SimpleDateFormat, which is the only subclass available.

DateFormat and SimpleDateFormat support parsing dates and also formatting Date objects to Strings. The next example shows how to do this:

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
 
Date parsedDate = df.parse("7.3.2013");
// Prints the parsed date object
System.out.println("The parsed date is " + parsedDate);
 
// Prints the current date in the pattern notation
System.out.println("The current date is " + df.format(new Date()));

First new SimpleDateFormat instance is created. The class takes a pattern String as the parameter. The pattern is used in the parsing phase to distinct which characters describe days and which describe months et cetera. You can see the whole list of supported pattern characters in the JavaDoc of SimpleDateFormat.

TIP If the date parsing returns wrong values double check your pattern. Wrong capitalization usually means you’ll get wrong values!

Next a Date instance is created by parsing a String with the parse method. As mentioned this method uses the given pattern. Some little differences are allowed between the input and the pattern, for example leading zeros are not mandatory for days and months but other than that the parameter string should be in the same format as the pattern. After the parsing the date is written to the console.

Finally the current date is written to the console with the format method. The format method uses the same pattern to create a string representation of the Date object.

Now when I ran this, the output I got was:

The parsed date is Thu Mar 07 00:00:00 EET 2013
The current date is 07.03.2013

Notice the difference between the outputted Dates. The reason for this is that in the first line the Date is written using the toString() method in the Date class and in the second line the DateFormat class is used. So as you can see the DateFormat class is the way to go when you want to control how the dates are outputted.

Using strict rules

As mentioned before the DateFormat.parse method allows for some differences between the input and the pattern. If you want to run the parsing in strict mode you can set lenient mode to false. This means that DateFormat won’t try to parse dates that are cleary wrong. One example of this is “32.3.2013”, in lenient mode this means the 32nd day after 1st of March 2013 e.g. 1st of April 2013. In strict mode the DateFormat won’t accept this and it throws an ParseException. Example below:

parsedDate = df.parse("32.3.2013");
System.out.println("The parsed date is " + parsedDate);
 
df.setLenient(false);
 
parsedDate = df.parse("32.3.2013");
System.out.println("The parsed date is " + parsedDate);

And the ouput:

The parsed date is Mon Apr 01 00:00:00 EEST 2013
Exception in thread "main" java.text.ParseException: Unparseable date: "32.3.2013"
  at java.text.DateFormat.parse(DateFormat.java:357)
  at net.korri.tutorial.java.dateparsing.DateParser.main(DateParser.java:27)

Further reading