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

TechTip: Use of setLenient method on SimpleDateFormat

DZone's Guide to

TechTip: Use of setLenient method on SimpleDateFormat

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Sometimes when you are parsing a date string against a pattern(such as MM/dd/yyyy) using java.text.SimpleDateFormat, strange things might happen (for unknown developers) if your date string is dynamic content entered by a user in some input field on the user interface and if it is not entered in the specified format.

The parse method in the SimpleDateFormat parses the date string that is in the incorrect format and returns your date object instead of throwing a java.text.ParseException. However, the date returned is not what you expect.

The below code-snippet shows you this behaviour.

 

package com.starwood.system.util;

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

public class DateSample {

    public static void main(String args[]){
        SimpleDateFormat sdf = new SimpleDateFormat () ;
        sdf.applyPattern("MM/dd/yyyy") ;
        try {
            Date d = sdf.parse("2011/02/06") ;
            System.out.println(d) ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

Output: Thu Jul 02 00:00:00 MST 173

 

See the output, that is a date back in the year 173.

To avoid this problem, call the setLenient (false) on SimpleDateFormat instance. That will make the parse method throw ParseException when the given input string is not in the specified format.

Here is the modified code-snippet.


package com.starwood.system.util;

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

public class DateSample {

    public static void main(String args[]){
        SimpleDateFormat sdf = new SimpleDateFormat () ;
        sdf.applyPattern("MM/dd/yyyy") ;
        sdf.setLenient(false) ;
        try {
            Date d = sdf.parse("2011/02/06") ;
            System.out.println(d) ;
        } catch (ParseException e) {
            System.out.println (e.getMessage()) ;
        }
    }

}

Output:
Unparseable date: "2011/02/06"



http://accordess.com/wpblog/2011/06/02/techtip-use-of-setlenient-method-on-simpledateformat

 

 

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:
java ,problems with simpledateformat ,setlenient ,simpledateformat

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 }}