-1

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

String str = "hello"                
user3005581
  • 31
  • 1
  • 4
  • `format` doesn't return a Date. Please post code that at least compiles (or ask a question about a compiler error). – user2864740 Nov 20 '13 at 06:12

3 Answers3

3

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);
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • i did as you told but i guess its giving error in following line:-Date date1 = df.parse(str); // i guess its not able to convert str(which is a string type) into date1(Date type). – user3005581 Nov 20 '13 at 06:23
  • On applying catch, catch(Exception e){ System.out.println("Caught " + e); } its showing following error still- parseException:unparseable date: – user3005581 Nov 20 '13 at 06:32
  • Thanks Masud. Its working now .Problem was"MM-dd-yyyy hh:mm:ss a" written instead of "dd-MMM-yyyy hh:mm a" – user3005581 Nov 20 '13 at 08:19
0

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

0

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);