0

I am new to Java. I have been trying to convert a date into format dd-MMM-yy. But i am getting exception :

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date

Below is my code . Please guide.

public class Test {
 public static void main(String args[ ])  {

     String currentDateString =new String();
     DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
     DateFormat dateFormatpdfname = new SimpleDateFormat("dd-MMM-yy");
        //Date currentDate = new Date();
       String dateInString = "Sep 16, 2018";
       String dateInString1 = "16-Sep-18";
        String currentDateVal=dateFormatpdfname.format(dateInString1);
        currentDateString = dateFormat.format(dateInString);
        System.out.println(currentDateVal);
        System.out.println(currentDateString);
}
}
ashishV
  • 23
  • 4
  • 2
    You should probably tell us what you want to achieve. You are passing a `String` to the `format` method while it expects a `Date` object. – DanielBarbarian Sep 19 '18 at 10:51
  • you are passing string to date object you can look into this link : - http://tutorials.jenkov.com/java-internationalization/simpledateformat.html – saurabhgoyal795 Sep 19 '18 at 10:56
  • Thanks al....l :) – ashishV Sep 19 '18 at 10:57
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 19 '18 at 11:21
  • Whenever you get an exception you don’t understand, it’s a good idea to paste the exception class name and message into your search engine. Often several helpful hints turn up. – Ole V.V. Sep 19 '18 at 11:25
  • 1
    @saurabhgoyal795 The tutorial you are linking to is using the long outdated and notoriously troublesome `SimpleDateFormat` class too. This is not recommended. Use for example [the *Parsing and Formatting* section](https://docs.oracle.com/javase/tutorial/datetime/iso/format.html) of the Oracle tutorial instead. – Ole V.V. Sep 19 '18 at 11:44
  • @OleV.V. thanks alot – saurabhgoyal795 Sep 19 '18 at 11:50

2 Answers2

1
Uncomment this 
//Date currentDate = new Date();
Then,
String currentDateVal=dateFormatpdfname.format(currentDate );
        currentDateString = dateFormat.format(currentDate );
Adya
  • 1,084
  • 9
  • 17
0

Probably i was not passing it as date and that is the reason i was getting error. Below is the correct answer.

 public class Test {
     public static void main(String args[ ]) throws ParseException  {
          //Base obj1 = new Base();
     // As per overriding rules this should call to class Derive's static 
     // overridden method. Since static method can not be overridden, it 
     // calls Base's display() 
          //Derived.display(); 
         Date myDate = null;
         String currentDateString =new String();
         DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
       //  DateFormat dateFormatpdfname = new SimpleDateFormat("dd-MMM-yy");
            //Date currentDate = new Date();
          // String dateInString = "Sep 16, 2018";
           String dateInString1 = "16-Sep-18";
           myDate = dateFormat.parse(dateInString1);
            //String currentDateVal=dateFormatpdfname.format(dateInString1);
            currentDateString = dateFormat.format(myDate);
            //String releaseDateStr = dateFormat.format(currentDateString);
        //  System.out.println(currentDateVal);
            System.out.println(currentDateString);
    }
    }
ashishV
  • 23
  • 4