• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalArgumentException

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written code like this:



When I run this piece of code, I get the following exception.

java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:279)
at java.text.Format.format(Format.java:133)
at Date.formatDate(Date.java:27)
at Date.main(Date.java:37)

Could somebody help me with this please??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the signatures on DateFormat. There are some that take a String argument like your "2005-02-10" and return a Date object. And Date is just what the format method expects as an argument. So you need a two step process:

String YYYY-MM-DD -> Date -> String MEDIUM
 
Abhinay Verma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayashree,
The following code can solve your problem:-

import java.text.SimpleDateFormat;
import java.text.ParsePosition;

public class dateFormatCode {
public static void main(String[] args){
String date = "2005-02-10";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
java.util.Date obj = formatter.parse(date,pos); //Convert to Date
String show = formatter.format(obj);
System.out.println(show);
}
}

If you want to display the entered date text in some other format, you can create it using SimpleDateFormat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic