Thursday, September 07, 2006

Java isDate() Function

New to Java, but learning fast and definitely experiencing the "Paradigm Shift", however I find it odd that there is no isDate() function like there is in Visual Basic.

As mentioned in an earlier post, I ran into a Java Date Conversion issue where the date format that I was expecting and turned out in the live data tests, to be a different format. This was after building over 500 test cases that test each of the business rules programmed into a Drools Rules (now Jboss) processor.

So obviously, I want to have all possible date formats accounted for so the solution is to have a function that loops through each of the possible date formats and check if the string parses into a valid Date object using that particular SimpleDateFormat.

I know this can be done using try-catch blocks, but I want to create a more elegant approach calling my custom isDate() function.

Checking aroung Javalobby and other places to see if anyone has already solved this, or if I need to create from scratch.

4 comments:

Anonymous said...

Nope, there's no such thing in default Java, but a quick search in Apache Commons turned up this: http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/time/DateUtils.html#parseDate(java.lang.String,%20java.lang.String[])

Anonymous said...

Take a look at this: http://www.apriori-it.co.uk/Javascript_VB_Library/Javascript_IsDate_Function.asp

Maybe you can port it to Java.

Blackfoot said...

Yeah, I saw that jakarta javascript on this as well, but I wasn't sure how to convert the js into the java format which is what I needed.

Blackfoot said...

I downloaded the apriori-it.co-uk javascript library but need this in java, not javascript. Check out my latest post, where this was solved with help from javalobby.