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);
}
}
7
  • 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. Commented Sep 19, 2018 at 10:51
  • you are passing string to date object you can look into this link : - tutorials.jenkov.com/java-internationalization/… Commented Sep 19, 2018 at 10:56
  • Thanks al....l :) Commented Sep 19, 2018 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. Commented Sep 19, 2018 at 11:21
  • 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 of the Oracle tutorial instead. Commented Sep 19, 2018 at 11:44

2 Answers 2

1
Uncomment this 
//Date currentDate = new Date();
Then,
String currentDateVal=dateFormatpdfname.format(currentDate );
        currentDateString = dateFormat.format(currentDate );
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.