public static boolean checkTimeFormat(String str){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.parse(str);
return true;
} catch (ParseException ex) {
System.out.println("not a valid time:"+str);
//ex.printStackTrace();
}
return false;
}
I have this method to check whether str in the correct format I tested it against
2015–01-01 07:01:14
and it says not a valid time. I am confused as they are in the same format.
Your example string has the wrong character for the first hyphen. It's an EN-DASH, aka unicode code point U+2013. Replacing it with a normal hyphen (U+002D) will work correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With