DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • The Complete Guide to Stream API and Collectors in Java 8

Trending

  • Serverless Machine Learning: Running AI Models Without Managing Infrastructure
  • *You* Can Shape Trend Reports: Join DZone's Data Engineering Research
  • Streamlining DevOps: How Containers and Kubernetes Deliver
  • Threat Modeling for Developers: Identifying Security Risks in Software Projects
  1. DZone
  2. Data Engineering
  3. Data
  4. TechTip: Use of setLenient method on SimpleDateFormat

TechTip: Use of setLenient method on SimpleDateFormat

By 
Upendra Chintala user avatar
Upendra Chintala
·
Jun. 27, 11 · Interview
Likes (5)
Comment
Save
Tweet
Share
46.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

 

 

Strings Data Types dev Interface (computing) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • The Complete Guide to Stream API and Collectors in Java 8

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: