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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • Lesson Learned on Groovy

Trending

  • Five Free AI Tools for Programmers to 10X Their Productivity
  • Writing Kubernetes Operators in Java: Part 1
  • Generative AI Unleashed: MLOps and LLM Deployment Strategies for Software Engineers
  • DevSecOps: Integrating Security Into Your DevOps Workflow
  1. DZone
  2. Data Engineering
  3. Data
  4. TechTip: Use of setLenient method on SimpleDateFormat

TechTip: Use of setLenient method on SimpleDateFormat

Upendra Chintala user avatar by
Upendra Chintala
·
Jun. 27, 11 · Interview
Like (5)
Save
Tweet
Share
46.20K 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
  • Lesson Learned on Groovy

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: