Here is a solution that uses the Xerces parser/validator. It uses .impl
classes however. Those are not part of the public API and subject to change. But if you stick to a certain version, you should be fine.
First, the dependency:
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>2.4.0</version>
</dependency>
And here is a small program that works like you described:
import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
import org.apache.xerces.impl.dv.xs.DateDV;
public class XSDValidator {
public static void main(final String[] args) {
System.out.println(isXSDDate("2012-09-18"));
System.out.println(isXSDDate("Hello World"));
}
private static boolean isXSDDate(final String string) {
try {
new DateDV().getActualValue(string);
return true;
} catch(final InvalidDatatypeValueException e) {
return false;
}
}
}
Output:
true
false
All you have to do yourself is to create methods for each of the data types. You should be able to find all the required classes in the org.apache.xerces.impl.dv.xs package.
Again, this is kind of abusing the Xerces library, as these classes are not part of the public API. So if you find another, cleaner solution, let us know.