SDF validating date input
Quote:
//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);
}
I am a beginner java student in school so I dont know much :\
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
Quote:
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);
THANKS TO ANYONE THAT CAN HELP ME. MY BRAIN IS ABOUT TO EXPLODE!