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

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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)


Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,simpledateformat ,java strings ,tutorial

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}