SimpleDateformatproblem.

Hi all,

i have a requirement where users has to enter the date in MM/DD/YYYY

format.So i have below statement

SimpleDateFormat format1 = new SimpleDateFormat("MM/DD/YYYY");

But even when i enter it date in 'MM/DD/YY' (09/09/06) it is parsing and not at all throwing an exception.

My requirement is when ever user enters date it has to compare with the format "MM/DD/YYYY" and if it is not maching it has to throw the exception.So I am wondering how to compare a date entered like 07/09/06(MM/DD/YY) with our required format "MM/DD/YYYY".

[584 byte] By [kanth218a] at [2007-10-3 2:51:44]
# 1
If possible can someone please provide code snippet for this.
kanth218a at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Look at setLenient(false) in SimpleDateFormat, and see if it helps.
MLRona at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 3
I have checked it.It still allowing the user to enter MM/dd/YY in place of MM/DD/YYYY.
kanth218a at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 4

I think you will have to parse out the values after the last '/' and see if it has four characters. Using this code:

try

{

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

sdf.setLenient(false);

Date date = sdf.parse("07/09/06");

System.out.println("Date: " + date);

}

catch (Exception e)

{

e.printStackTrace();

}

does not thow an exception.

Also, make sure the values you pass into the SimpleDateFormat are correct. There is a difference between a 'D' and a 'd'. Same goes for 'Y' and 'y'.

fitz

fitzbenfielda at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 5
To ensure that the whole string was consumed on parse use the parse method that takes a ParsePosition, then verify that the index == length of the entered string.Good LuckLee
tsitha at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 6
User can enter the data with out "/" also.the field accepts date with out "/" and when we click on tab it automatically inserts "/"(done by using javascript).
kanth218a at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 7
> User can enter the data with out "/" also.the field> accepts date with out "/" and when we click on tab it> automatically inserts "/"(done by using javascript).Maybe you could check the number digits in the year is 4 when you parse it with the javascript?
zadoka at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 8
Any other ideas plz
kanth218a at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...
# 9
If there's a requirement for exactly 10 characters then check that before even trying the SimpleDateFormat.
DrClapa at 2007-7-14 20:40:38 > top of Java-index,Java Essentials,Java Programming...