Results 1 to 3 of 3
Thread: SDF validating date input
- 02-02-2011, 09:44 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
SDF validating date input
I am a beginner java student in school so I dont know much :\//ask for date code
Scanner sd = new Scanner(System.in);
sd.useDelimiter("/");
System.out.println("Please enter the date in MM/DD/YYYY format:");
String input = sd.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
int month = 0;
int day = 0;
int year = 0;
String fullDate = "";
try
{
sdf.parse(input);
sd = new Scanner(input);
sd.useDelimiter("/");
month = sd.nextInt();
day = sd.nextInt();
year = sd.nextInt();
fullDate = + month + "/" + day + "/" + year;
//System.out.println(fullDate);
}
catch (Exception err)
{
System.out.println("Invalid Date.");
System.out.println(err);
}
So for this small block of code I am having trouble getting it to accept the year as EXACT 4 digits. for example, the user can type in 2/2/11 and my code won't catch it. I want the user to only be able to type 2/2/2011 or 02/02/2011 for example SO THEN the code is perfect. one more question is I want it to loop back to the beginning of the try statement if there a mistake that is caught.
The reason I want it to be 2011 is because I am using a calendar.. and it affects the values if it isnt 2011 and is 11. which is below
THANKS TO ANYONE THAT CAN HELP ME. MY BRAIN IS ABOUT TO EXPLODE!sd = new Scanner(input);
System.out.println("Service date: " + fullDate);
String svcDateIn = sd.nextLine();
sd = new Scanner(svcDateIn);
sd.useDelimiter("/");
month = month - 1;
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
int n = cal.get(Calendar.DAY_OF_WEEK);
System.out.println("Number of day is " +n);
- 02-02-2011, 06:22 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
no one can see why my validation doesnt work??
- 02-03-2011, 08:37 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
From the API:
"For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields."
In other words you can't do it with just the SDF.
The usual thing is to either check the string beifre formatting (split() and check the length of the bits you want to check), or limit what the user can enter by not actually allowing them to type in a date. That is, use a calendar pop-up, or a set of drop down selections, that sort of thing.
Similar Threads
-
trouble validating 2 conditions
By cottoneye1256 in forum New To JavaReplies: 2Last Post: 08-23-2010, 06:53 PM -
Validating Jtable
By anilkumar_vist in forum Advanced JavaReplies: 1Last Post: 11-24-2009, 02:54 PM -
Validating url with regular expression??
By kirtichopra2003 in forum Advanced JavaReplies: 2Last Post: 10-07-2009, 01:29 PM -
validating account
By ServletEst in forum Java ServletReplies: 3Last Post: 08-24-2009, 01:11 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks