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

  • SEARCH
  • Home
  • 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 5989931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:10:17+00:00 2026-05-22T23:10:17+00:00

I have the code below and it works pretty good except if you enter

  • 0

I have the code below and it works pretty good except if you enter something like 2/2/2011, you get the error message “The Document Date is not a valid date”. I would expect that it would say “The Document Date needs to be in the format MM/DD/YYYY”.

Why does the line newDate = dateFormat.parse(date); not catch that?

// checks to see if the document date entered is valid
    private String isValidDate(String date) {

        // set the date format as mm/dd/yyyy
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date newDate = null;

        // make sure the date is in the correct format..        
        if(!date.equals("mm/dd/yyyy")) {
            try {
                newDate = dateFormat.parse(date);
            } catch(ParseException e) {
                return "The Document Date needs to be in the format MM/DD/YYYY\n";
            }

            // make sure the date is a valid date..
            if(!dateFormat.format(newDate).toUpperCase().equals(date.toUpperCase())) {
                return "The Document Date is not a valid date\n";
            }

            return "true";
        } else {
            return "- Document Date\n";
        }
    }

EDIT: I’m trying to enforce strict adherence to the format MM/DD/YYYY. How can I change the code so that if a user enters “2/2/2011”, it will display the message: “The Document Date needs to be in the format MM/DD/YYYY”?

  • 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-05-22T23:10:18+00:00Added an answer on May 22, 2026 at 11:10 pm

    As already mentioned, the SimpleDateFormat is able to parse "2/2/2011" as if it is "02/02/2011". so no ParseException is thrown.

    On the other hand, dateFormat.format(newDate) will return "02/02/2011" and is compared against "2/2/2011". The two strings aren’t equal, so the second error message is returned.

    setLenient(false) will not work in this case:

    Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

    Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it’s needed to separate two adjacent fields.

    (source: java docs)

    you can use a regular expression to manually check the string format:

    if(date.matches("[0-9]{2}/[0-9]{2}/[0-9]{4}")) {
        // parse the date
    } else {
        // error: wrong format
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far i have got the code below which works lovely when trying an
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
The code below works for Delphi XE, but the 2400 buffersize is pretty ugly.
I have code like below to perform some conditional validation on fields in my
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have a table defined (see code snippet below). How can I add a
I have noticed that my geocoder is inconsistent in the code shown below because
Okay so I have a scenario similar to the below code, I have a
The code I have pasted below is meant to display images on the middle

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.