• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

validating day and month using SimpleDateFormat

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can use DateFormat's setLenient method.
 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Jeff.

I tried to put formatter.setLenient(false); in my code, but now i get a different error when i execute my code.

now the output i get is

when i enter a wrong date 15/12/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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you wrote in your last post is flawed: if the date string is invalid,
parse throws an exception, but you catch it and continue with a null date, so it's not suprising that a NullPointerException is around the corner. The simplest thing to do is report an error like "invalid date". Being able to report "invalid month" versus "invalid day for given month" is going to be more trouble than it's worth, imho.
 
Get me the mayor's office! I need to tell her about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic