1

Validating the given date against the format dd/mm/yyyy.
Valid = 1
Not Valid = -1
Example1:
Input= 12/06/1987
output=1
Example2:
Input= 03/1/1987
output=-1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CheckDateFormat {
    public static void main(String[] args) {
        String s1="29/02/2006";
        getvalues(s1);
    }
    public static void getvalues(String s1) {
        if(s1.matches("[0-9]{2}[/][0-9]{2}[/][0-9]{4}"))
        {
            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
            sdf.setLenient(false);
            try {
                Date d1=sdf.parse(s1);
                System.out.println(1);
            } catch (ParseException e) {
                //e.printStackTrace();//Always going to catch block
                System.out.println(-1);
            }
        }
        else
            System.out.println(-1);
    }
}


The problem with the code is that it it always returns -1.
It always enters the catch block and gives a ParseException.
Is there any problem with the Regex?

3
  • 1
    you should do your homework by yourself. search for online regex testers and try it there Commented Oct 19, 2015 at 7:47
  • 1
    You're also confusing "returning" and "printing". You should also inform your teacher that Java has a boolean type, that should be used here instead of 1/-1. And it enter the catch block because there are only 28 days in february 2006. So the date 29/02/2006 is invalid. Commented Oct 19, 2015 at 7:49
  • Are you trying to validate date or just validate the format? If you are validating date strictly the exception you got was correct ,since 29/02/2006 is invalid. Commented Oct 19, 2015 at 8:36

4 Answers 4

8

Problem is this line:

sdf.setLenient(false);

and this date:

String s1="29/02/2006";

As 2006 wasn't a leap year making your date invalid and with lenient set to false date parse call is failing and throwing ParseException.

Problem will be fixed if you comment out sdf.setLenient(false); line:

or use a valid date:

String s1="29/02/2008";

Since 2008 was a leap year making 29th Feb a valid date.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes it did work. Problem was with leap year date. Got to learn through my mistake. Thank you
Sorry friend I still don't have 15 reputation so can't vote. Will mark it as soon as i can. Thank you once again
No you don't need 15 reps to accept an answer. Click on the link I provided in comment above to see how to accept an answer. You just need to click the tick mark on top-left of my answer and it will turn green. You will also get 2 points for that.
0

You do not need a regex. If you get a ParseException you can return -1 else you return 1:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CheckDateFormat {
    public static void main(String[] args) {
        String s1="29/02/2006";
        System.out.println(getvalues(s1));
    }
    public static int getvalues(String s1) {
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        try {
            Date d1=sdf.parse(s1);
            System.out.println(1);
        } catch (ParseException e) {
            return -1;
        }
        return 1;
    }
}

Comments

0

Exception is thrown due to following line. Comment it off.

sdf.setLenient(false);

Comments

0

As the year 2006 was not leap year, using String s1="29/02/2006"; will throw a parse exception.

Try with other dates it will work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.