wood burning stoves 2.0*
The moose likes Java in General and the fly likes Date Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Validation" Watch "Date Validation" New topic
Author

Date Validation

Matt Hoffman
Ranch Hand

Joined: Jun 03, 2004
Posts: 83
I am trying to validate a date like such.. yyyy-mm-dd. I am using this following. The date will turn out to a funny number 1043-22-02 instead of a normal year, month, date. Any suggestions

public final class DateValidation
{

protected static boolean isDate(String string)
{
if (string == null)
return false;

if (string.indexOf("/") > -1)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
sdf.setLenient(false);
Date dt = sdf.parse( string );

return true;
}
catch(Exception e)
{
return false;
}
}
else
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.setLenient(false);
Date dt = sdf.parse( string );

return true;
}
catch(Exception e)
{
return false;
}
}
} //isDate
}
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
Try using MM instead of mm. Lowercase m is minutes in hour.


Author of Java 8 for the Really Impatient
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
y Year Year 1996; 96
M Month in year Month July; Jul; 07
D Day in year Number 189
d Day in month Number 10



this will help you.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Validation
 
Similar Threads
No exception when parsing date from string
Validating Dates
Why is 01/01/2000 unparsable?
String to date format
date format