Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating day and month using SimpleDateFormat 1

Status
Not open for further replies.

vishnuparimi

Programmer
Dec 28, 2005
4
US
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.
 
It looks like it's throwing an error on the data parsing:

try{
parseDate = formatter.parse(vGrantdate);
System.out.println(parseDate);
}catch(ParseException ex){
System.out.println(ex.getLocalizedMessage());
}
and your getting the LocalizedMessage

The only thing suspicious is where you say:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
rather than:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

_________________
Bob Rashkin
 
I don't think it is throwing an error...it is parsing your date as the 15th month of the year 2005, which if you think about it is actuall March of 2006. All your subsequent calculations are being performed on the newly parsed date of March 30, 2006.

I would not advise you try to correct this, but rather nip it in the bud.

A month value of 15 is obviously invalid, so I would attempt to cut that off at the source. If it's an input field, validate the user's entry before you try to use it.
Could you give more info as to the data's source?

Rich
 
Thanks for the reply Rich. You are right when i am entering a wrong date, for example 15/23/2005, it is converting that to 3/23/2006, and giving the day and month based on the new calculataions. But what i want in my code is if there is a wrong day or month entered, i wanted to display a message saying, the month is invalid(it should be between 1 and 12) or day is invalid( it should be between 1 and 28 based on the month).
I actually came to know that we can you setLenient(boolean)
method through which, if i put setLenient(false), then it will not parse the date if i enter a wrong date ex:15/23/2005, but in that case it will throw me a parseexception in the begining it self, so that the date will be null when it enters to at the point dtCalendar.setTime(parseDate);
pl go thru my code..
Code:
String vGrantdate = "15/30/2005";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
formatter.setLenient(false);
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);//1 Here i am getting the error
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);
}
now the output i get is
Code:
java.lang.NullPointerException	
  at java.util.Calendar.setTime(Calendar.java:879)	
  at package1.DateValidation.main(DateValidation.java:47)Exception in thread "main" Unparseable date: "15/30/2005"

when i enter a wrong date 15/30/2005, dtCalendar.setTime(parseDate);
dtCalendar is not setting this wrong date i guess.

Can any one pl explain me how to set a wrong date so that i can do the validations for month and day if they are more than the max allowed month/day.

Thanks very much.
 
Personally, I would stop execution when I got to the parsing exception. It almost seems as if you are trying to do the same job twice. The parse exception is telling you that you have entered an invalid date. Since the initial value you set for parseDate is null, you should not attempt to use this field until you are certain it is initialized to an acceptable value.

Based on the way you catch your exception, you should stop execution at the Exception and advise your user the date entry was invalid. If your desire is to tell them exactly what is wrong with their entry, then my first suggestion would be to run the date entry through your own filter BEFORE you try to parse it into a Date. You then would have full control over the date entry, and can set your filter to watch out for not only non-existent dates, but dates that do not fit into any other criteria your application requires.

Hope this helps.

Rich
 
Rich,
Yes you are right. It would be better if I stop my execution soon after I get ParseException, and show a customized message saying invalid date. But in my case, the requirement is, I need to be specific as to which field is entered wrong. and display where the user entered wrong value. So I need to do day and month validations too.
From your post, I did not understand about the filter that you were talking about. Can you Pl explain about this..

Thanks for your help.
 
This is quick, dirty and untested, but something along these lines:

Code:
public class MyDateEntryFilter{

  public void validateEntry(String dateEntry) throws FormatException{
    //assuming the only accepted format is "MM/dd/YYYY"
    try{
      validateMonth(dateEntry);
      //day .. year
    }
    catch(FormatException e){
      throw(e)
    }
  }

  private void validateMonth(String entry) throws FrormatException{
    String month = entry.subString(0,2)
    String slash = entry.subString(2,1)
    if(Integer.parseInt(month)<1 || Integer.parseInt(month)>12){
      //blow up
    }
}

//Do the same for day and year

That should get you moving...

Rich
 
Rich,

Thank you very much for the reply. It works for me..

Thankyou.
Visnu.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top