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

前端 未结 4 1963
南方客
南方客 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:02

    I have the following exception:

    java.text.ParseException: Unparseable date

    System.out.println("inside the XMLGregorianCalendar");
    
    sb = (String) map.get(fieldname);
    System.out.println("THis is XMLGReogoriaaaaan calendar"+ sb);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    Date journeyDate = new java.sql.Date(df.parse(sb).getTime());
    System.out.println("this"+journeyDate);
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
  • 2021-01-03 05:08
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
    Date journeyDate = df.parse(date); // gives you java.util.Date
    

    If you want java.sql.Date then

    java.sql.Date sqlDate = new java.sql.Date(journeyDate.getTime());
    
    0 讨论(0)
  • 2021-01-03 05:13

    You have mixed m and M.

    mstands for minute and M for month.

    Below is an example of a working format.

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    String dateInString = "07/06/2013"; 
    Date date = formatter.parse(dateInString);
    System.out.println(formatter.format(date));
    
    0 讨论(0)
提交回复
热议问题