SimpleDateFormat
seems to ignore the strings in the matching string once it has found the matching pattern.
For example: the pattern "yyyyMMddHHmm"
is matching 201601251531oi
which I suppose it should not. However this 20160125153133
fails which is exactly right.
I tried to setLenient
as true
and has no effect. I know we can add another check for number of digits (regex), but is there way to strictly make this pattern only work using SimpleDateFormat
alone ?
public static boolean isValidDateTimeFormat(String anyString,
String dateTimePattern) {
// here the pattern is 'yyyyMMddHHmm'
SimpleDateFormat datePattern = new SimpleDateFormat(dateTimePattern);
datePattern.setLenient(false); // no difference
try {
datePattern.parse(anyString);
return true;
}
catch (Exception pe){
return false;
}
}