This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes IllegalArgumentException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "IllegalArgumentException" Watch "IllegalArgumentException" New topic
Author

IllegalArgumentException

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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??


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abhinay Verma
Greenhorn

Joined: Dec 13, 2004
Posts: 24
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalArgumentException