Date error checking

D

dgfgrdg

java.sql.Date sqldate = java.sql.Date.valueOf( year+"-"+month+"-"+day );

How can I check if a date is vaild because using this day=38 month=15 is
still vaild.
 
R

Rhino

dgfgrdg said:
java.sql.Date sqldate = java.sql.Date.valueOf( year+"-"+month+"-"+day );

How can I check if a date is vaild because using this day=38 month=15 is
still vaild.

I have a class full of static utility methods for dealing with dates. This
is the method I use for verifying that a String is a valid ISO date, i.e. a
date in YYYY-MM-DD format, such as 2006-05-17 (May 17, 2006):

=================================================================

/**
* Converts a date expressed as a String into a java.sql.Date.
*
* <p><i>Example:</i><br>
* <xmp>
* java.sql.Date mySqlDate = DateTimeUtils.toSqlDate("2005-02-17");
//should return an SQL date equivalent to the input ISO date.
* </xmp></p>
*
* <p><b>NOTE: </b>Although the input parameter expresses a date with
only a year, month and day,
* the date returned by this method includes a year, month, day, hour,
minute, second, and milliseconds.
* The date returned by this method will have the same year, month, and
day as the input parameters
* while the hours, minutes, seconds, and milliseconds will all be zero,
which effectively means
* midnight on the date (year, month, and day) in question.</p>
*
* @param date a date given in ISO (YYYY-MM-DD) format.
* @return java.sql.Date expressed with a default time of midnight.
*/
static public java.sql.Date toSqlDate(String date) {

java.text.SimpleDateFormat sdf = new
java.text.SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
sdf.setLenient(false);

java.util.Date utilDate = null;
try {
utilDate = sdf.parse(date);
} catch (ParseException p_excp) {
throw new IllegalArgumentException("Encountered ParseException
at offset " + p_excp.getErrorOffset() + " while attempting to convert the
String " + date + " to a java.sql.Date. Details: " + p_excp.getMessage());
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

return (sqlDate);
}

=================================================================

The initiatlization of the SimpleDateFormat describes the format of the
incoming date using the symbols expected by the SimpleDateFormat class (see
the API for a description).

The line:

sdf.setLenient(false);

is very important. It ensures that a date with a month of 15 and a day of 38
or other similar absurdities are not accepted as valid. See the setLenient()
method in the API for more of an explanation.

The next lines try to create a java.util.Date using the sdf.parse() method
on the incoming String; if the incoming String represents a valid date, the
java.util.Date is then converted to a java.sql.Date and is returned;
otherwise, the catch block identifies the error.

Naturally, you don't have to handle the error by throwing an
IllegalArgumentException, that's just my preferred action in this case.

You'll find that the message from the ParseException isn't particularly
specific about what's wrong with the date but it's better than nothing. For
instance, when I used "2004-15-38" as the input String, the ParseException
message was: Unparseable date: "2004-15-38". I would have preferred
something that specifically said that the month and/or day was out of range.
The full error message thrown by the IllegalArgumentException was a little
better but still not great:

Encountered ParseException at offset 10 while attempting to convert the
String 2004-15-38 to a java.sql.Date. Details: Unparseable date:
"2004-15-38"

If you want a detailed analysis of exactly why the date is invalid, you have
to use a rather different approach that inspects each part of the incoming
String. If you need code that will do that, post again and maybe someone
here can help. But if you just need to know if a given date is valid or not,
the code I've given you should be fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top