简体   繁体   中英

java.text.parseException:unparsable date

i am getting this error while i am trying to convert a string into date. unparasable data Below is my code:-

String str = "hello"                

Second is missing at your parse String str . So, to parse it you should not include second format at SimpleDateFormat pattern . Also correct the day and Month format. Look at the declaration of df

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss

To know details of pattern, go through this docs .

Edit

String date2 = sdformatter.format(date1);// format method return String.
                                         //Should declare with String

Full Code

    String str = "25-Nov-2013 06:00 AM";
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
    SimpleDateFormat sdformatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
    Date date1=null;
    try {
        date1 = df.parse(str);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    String date2 = sdformatter.format(date1);
    System.out.println(date2);

According to str format you should write your SimpleDateFormat,

(25-Nov-2013 06:00 AM ---> dd-MMM-yyyy hh:mm a) and for 
(25-Nov-2013 06:00:30 AM-----> dd-MMM-yyyy hh:mm:ss a)  

will work

Try this

long newerdate = new Date().parse("25-Nov-2013 06:30 AM");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("dd-MM-yyyy hh:mm a");
String data = df.format(newerdate);
System.out.println(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM