File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "IllegalArgumentException" Watch "IllegalArgumentException" New topic


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(
at java.text.Format.format(
at Date.formatDate(
at Date.main(

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

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

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:
subject: IllegalArgumentException
It's not a secret anymore!