java.text.ParseException: Unparseable date: convert mm/dd/yyyy string to a date

前端 未结 4 1962
南方客
南方客 2021-01-03 04:18

when i convert my string object in mm/dd/yyyy format to Date it gives me

java.text.ParseException: Unparseable date: \"09/17/2014         


        
4条回答
  •  执笔经年
    2021-01-03 05:03

    There are several potential problems here:

    • You're not specifying a format
    • You're not specifying a locale
    • You're not specifying a time zone
    • You're trying to cast the return value (which will be a java.util.Date reference) to a java.sql.Date - that would fail

    You want something like:

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    df.setTimeZone(...); // Whatever time zone you want to use
    Date journeyDate = new java.sql.Date(df.parse(text).getTime());
    

提交回复
热议问题