Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Gotcha: Parsing Strings Using SimpleDateFormat

DZone's Guide to

Java Gotcha: Parsing Strings Using SimpleDateFormat

Java takes pains to parse dates a certain way, which can lead to problems when parsing strings from the SimpleDateFormat. The setLenient method can help with that.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

The other day, I was trying to parse a date string into a date object using SimpleDateFormat to check for the validity of the date string. I had the SimpleDateFormat defined as: 

SimpleDateFormat expiryDateFormat = new SimpleDateFormat("dd/MM/yyyy");

The date string I was trying to parse was: 

10/26/2016

Which is clearly invalid with respect to the pattern defined in the SimpleDateFormat.

I found that the parsing went through and then it failed while trying to insert into my DB due to the invalid date. I was utterly confused. Then, I wrote a small program to isolate the issue with SimpleDateFormat:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Solution {

  public static void main(String[] args) {

    SimpleDateFormat expiryDateFormat = 
        new SimpleDateFormat("dd/MM/yyyy");
    try {
      Date date =expiryDateFormat.parse("10/26/2016");
      System.out.println(date);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}


The above code gave the output: Sat Feb 10 00:00:00 AST 2018. I was surprised at this behavior. Then, I uncovered a secret method setLenient(), which we can use to set the SimpleDateFormat parsing to be strict and report errors on slightest of mismatch and not try to interpret the input by adjusting the value. So, my updated code looks like:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Solution {

  public static void main(String[] args) {

    SimpleDateFormat expiryDateFormat = 
        new SimpleDateFormat("dd/MM/yyyy");
    expiryDateFormat.setLenient(false);
    try {
      Date date =expiryDateFormat.parse("10/26/2016");
      System.out.println(date);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}


Which indeed throws an exception as expected:

java.text.ParseException: Unparseable date:"10/26/2016"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at Solution.main(Solution.java:12)


BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
java ,simpledateformat ,java strings ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}