Friday, September 08, 2006

Java isDate() Solved!

With the help of some friends in Javalobby I was able to get a solution to the isDate() issue for Java.
Below is a method that simulates the Visual Basic isDate() method and is configurable so you can set what you consider are the valid date formats.
I modifed this and added the "Jan|Feb|Mar|Apr..." and "JAN|FEB|MAR|APR|..." to the regex so that the method now validates the formats I needed to check.
I am now implementing this in my application.

-------------------------------------------------

import java.util.ArrayList;
import java.util.regex.Pattern;

public class IsDateTestClass {

public static boolean isDate(CharSequence date) {

// some regular expression
String time =
"(\\s(([01]?\\d)|(2[0123]))[:](([012345]\\d)|(60))"
+
"[:](([012345]\\d)|(60)))?"; // with a space before, zero or one time

// no check for leap years (Schaltjahr)
// and 31.02.2006 will also be correct
String day =
"(([12]\\d)|(3[01])|(0?[1-9]))"; // 01 up to 31
String month =
"((1[012])|(0\\d))"; // 01 up to 12
String year =
"\\d{4}";

// define here all date format
ArrayList patterns = new ArrayList();
patterns.add(Pattern.compile(day + "[-.]" + month + "[-.]" + year + time));
patterns.add(Pattern.compile(year + "-" + month + "-" + day + time));
// here you can add more date formats if you want

// check dates
for (Pattern p : patterns)
if (p.matcher(date).matches())
return true;

return false;

}

public static void main(String[] args) {

ArrayList dates = new ArrayList();
dates.add("05.10.1981"); // swiss date format (dd.MM.yyyy)
dates.add("05-10-1981");
dates.add("07-09-2006 23:00:33");
dates.add("2006-09-07 23:01:24");
dates.add("2003-08-30");
dates.add("2003-30-30"); // false
dates.add("some text"); // false

for (String d : dates)
System.out.println(isDate(d));

}

}
-------------------------------------------------


8 comments:

Anonymous said...

Is it just my browser or is the Green script virtualy unreadably in others

Blackfoot said...

Ok, ok, I changed my layout. Are you happy now?

Anonymous said...

Thank you so much for posting this. It has helped me immensly in trying solve a crucial data validation issue.

Manish Danani
ldana01@dcs.bbk.ac.uk
MScAIS optimization search

Blackfoot said...

I am glad I could help.

Roman KIrillov said...

is blog still alive? I've seen some sacred letters MScAIS search optimization here!

Anonymous said...

You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

Patrick said...

I tend to need support for checking both 01/01/2010 and 1/1/2010, so added the following modification:

// no check for leap years (Schaltjahr)
// and 31.02.2006 will also be correct
String day = "(([12]\\d)|(3[01])|(0?[1-9])|([1-31]))"; // 01 up to 31 or 1 to 31
String month = "((1[012])|(0\\d)|([1-12]))"; // 01 up to 12 or 1 to 12
String year = "\\d{4}";

Blackfoot said...

Nice addition Patrick