aspose file tools*
The moose likes Servlets and the fly likes How to check if the given date is valid?using  SimpleDateFormat.Is this short and rig Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to check if the given date is valid?using  SimpleDateFormat.Is this short and rig" Watch "How to check if the given date is valid?using  SimpleDateFormat.Is this short and rig" New topic
Author

How to check if the given date is valid?using SimpleDateFormat.Is this short and rig

Seema Shetty
Greenhorn

Joined: Jan 23, 2001
Posts: 9
Hi,
I'am trying to check if the given date is a valid with the following code.
Is this the right way of doing this?
If so,after I get the day, month and year values using the FieldPosition object, is there a built in java method, which tells me that the given String date is a valid date.
One more problem I have with this is, if I enter the date 02/0398/199999, it should give me the exception.But this code parses this date and gives the output for month=3,day=4,year=200000.
But according to the requirement, that should be invalid date.
If this is not the right way, how can I proceed to achieve my results .Please get me the clue about what all the methods i need to use and what is the sequence.May be Calendar method or something else.
public class hello extends HttpServlet {
String sDate="02/0398/199999"; Date date;
int MONTH; int DAY; int YEAR;


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// DatePattern datepattern;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();

SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy");
try{
date = dateformat.parse(sDate);
out.println("date is parsed" + date);

}
catch (ParseException ee) {
out.println(ee.getMessage());
}
FieldPosition posMonth = new FieldPosition(DateFormat.MONTH_FIELD);
FieldPosition posDay = new FieldPosition( DateFormat.DATE_FIELD);
FieldPosition posYear = new FieldPosition(DateFormat.YEAR_FIELD);

StringBuffer yearBuf = new StringBuffer();
StringBuffer monthBuf = new StringBuffer();
StringBuffer dayBuf = new StringBuffer();
try{
yearBuf = dateformat.format(date,yearBuf,posYear);
monthBuf = dateformat.format(date, monthBuf, posMonth);
dayBuf = dateformat.format(date, dayBuf, posDay);

}
catch (Exception ex1) {
out.println(ex1.getMessage());
}
int mBegin = posMonth.getBeginIndex();
int mEnd = posMonth.getEndIndex();
int dBegin = posDay.getBeginIndex();
int dEnd = posDay.getEndIndex();
int yBegin = posYear.getBeginIndex();
int yEnd = posYear.getEndIndex();
try{

MONTH = Integer.parseInt(monthBuf.substring(mBegin, mEnd));
DAY = Integer.parseInt(dayBuf.substring(dBegin, dEnd));
YEAR = Integer.parseInt(yearBuf.substring(yBegin, yEnd));

}
catch(Exception ne)
{
out.println(ne.getMessage());
}

int mEntered = 0;
int dEntered = 0;
int yEntered = 0;
// traps a number format exception if syntax is bad
try{
mEntered = Integer.parseInt(sDate.substring(mBegin, mEnd));
dEntered = Integer.parseInt(sDate.substring(dBegin, dEnd));
yEntered = Integer.parseInt(sDate.substring(yBegin, yEnd));


}
catch (Exception nee)
{
out.println(nee.getMessage());
}
if ( YEAR == yEntered && DAY == dEntered && MONTH == mEntered )
{

}
}
}
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi,
Without writing the code here, but I think you can just take the string, using StringTockenizer(String,"/") brake it up in three strings and check each string individually so:
month.length() = 2,
day.length() = 2,
year.length() = 4?
------------------
Alex
"Java is the answer"


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Seema Shetty
Greenhorn

Joined: Jan 23, 2001
Posts: 9
Thanks.
Are there any built in java methods to validate the date.
Also, how can we do the same using the methods in java.text.SimpleDateFormat,java.util.Date,java.text.DateFormat,
java.text.ParsePosition,java.text.FieldPosition,
java.text.ParseException.
Originally posted by Alex Kravets:
Hi,
Without writing the code here, but I think you can just take the string, using StringTockenizer(String,"/") brake it up in three strings and check each string individually so:
month.length() = 2,
day.length() = 2,
year.length() = 4?

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
you just have to look at API for these classes and see which one works for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check if the given date is valid?using SimpleDateFormat.Is this short and rig