Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:48:27+00:00 2026-06-05T20:48:27+00:00

I wrote a little piece of code to expose my problem. public class date

  • 0

I wrote a little piece of code to expose my problem.

public class date {
    public static void main(String args[]) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH);
        System.out.println("The date format is : dd-MM-yyyy.");
        String date1 = "20-06-2012";
        System.out.println("The date1 is : " + date1);
        String date2 = "2012-06-20";
        System.out.println("The date2 is : " + date2);
        try {
            System.out.println(formatter.parse(date1).toString());
            System.out.println(formatter.parse(date2).toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The output looks like so :

The date format is : dd-MM-yyyy.
The date1 is : 20-06-2012
The date2 is : 2012-06-20
Wed Jun 20 00:00:00 EDT 2012
Mon Dec 03 00:00:00 EST 25

The problem is that I want to have an error raised when the date submitted doesn’t match the pattern specified in the SimpleDateFormat, unfortunately, it looks like it sees numbers at the correct position in the string separated by the dashes so it makes it through. Is there another tool to do this or am I wrongly using SimpleDateFormat?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:48:28+00:00Added an answer on June 5, 2026 at 8:48 pm

    Use SimpleDateFormat.setLenient(boolean) and set to false to bypass SDF parse heuristics.

    Then switch to Joda-Time 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple little piece of Lua code, which I wrote while
Here's a little piece of code I wrote to report variables with missing values
Some time ago I wrote a little piece of code to ask about on
I am very new to classic ASP. I wrote a little piece of code
I'm trying to write a little piece of code to show in a portlet
I wrote little WPF application with 2 threads - main thread is GUI thread
i wrote a little addin, which does some formatting of my C# code. in
Hey, I've got this nice little piece of code, much like all the other
Im trying to learn glib/gtk. I wrote little code which prints files in directory
I wrote a little PyGTK app: Workcycler Now I have the problem that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.