Hi Everyone,
I have a question to ask regarding the SimpleDateFormat class.
In my code i am having a date
string and i am converting it to a date. and validating if the month entered is greater than max month and if day is greater than max day. If so i want to throw the error.
Here is my piece of code.
String vGrantdate = "15/30/2005";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date parseDate = null;
try{
parseDate = formatter.parse(vGrantdate);
System.out.println(parseDate);
}catch(ParseException ex){
System.out.println(ex.getLocalizedMessage());
}
dtCalendar = Calendar.getInstance();
dtCalendar.setTime(parseDate);
int nDay = dtCalendar.get(Calendar.DAY_OF_MONTH);
int nMaxDay = dtCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int nMonth = dtCalendar.get(Calendar.MONTH);
int nMaxMonth = dtCalendar.getActualMaximum(Calendar.MONTH);
if (nMonth > nMaxMonth) {
System.out.println("The month is incorrect");
}else{
System.out.println("month is :" + nMonth + "Max month is:" +nMaxMonth );
}
if (nDay > nMaxDay) {
System.out.println("The day is incorrect");
}else{
System.out.println("day is :" + nDay + "Max day is: " + nMaxDay);
}
here i get the output as
"Thu Mar 30 00:00:00 CST 2006"
month is :2 Max month is: 11
day is :30 Max day is: 31
where in i expect the output to be
The month is incorrect
The day is incorrect
Can any pl explain how to get this.
Thanks in advance.