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
Please enter at least three characters to search
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Integrating Security as Code: A Necessity for DevSecOps
  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.8K 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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!