Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simpledataformat not recognizing valid string

  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.

like image 478
RhumB Avatar asked Mar 12 '23 09:03

RhumB


1 Answers

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.

like image 156
izrik Avatar answered Mar 23 '23 13:03

izrik