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 single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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)


CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}