IllegalArgumentExceptio - Date

Hi friends,

i need to set the date (2007,04,01 (yyyy,MM,dd)) to calendar class but it throws IllegalArgumentException

Calendar calEx1 = Calendar.getInstance();

calEx1.set(2007,04,01);

System.out.println("Set value : " + format.format(calEx1.getTime()));

Date dateObject =new Date();

dateObject = calEx1.getTime();

the below line are used in another class, i am just pass the dateObject (date object) to that class.

Date ex1 =new Date();

ex1 = dateObject;

Calendar fromServer = Calendar.getInstance();

// here i am trying to set the date from that class

fromServer.set(ex1.getYear(),ex1.getMonth(),ex1.getDate());

System.out.println(format.format(fromServer));

Thank you,

[901 byte] By [MCA134a] at [2007-11-27 3:42:01]
# 1

You give a Calendar object as parameter to the format method. You should provide a Date:System.out.println(format.format(fromServer));

should probably beSystem.out.println(format.format(fromServer.getTime()));

Note that as you have a Date object, you can use the Calendar.setTime(Date) method (you have probably noticed that the getYear, getMonth, etc. methods of Date are deprecated.)

Note that if you don't need to manipulate calendar fields, it is likely that you don't need a calendar (but I don't know what your actual code does.)

TimTheEnchantora at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 2

> // here i am trying to set the date from that class

> fromServer.set(ex1.getYear(),ex1.getMonth(),ex1.getDate());

The getDate() method returns a java.util.Date object, not a day of the month.

fromServer.setTime(ex1.getDate());

It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of [url=http://java.sun.com/javase/reference/api.jsp]online documentation[/url] that you can even [url=http://java.sun.com/javase/downloads/index.jsp]download[/url] for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

[url=http://java.sun.com/javase/reference/api.jsp]Java?API Specifications[/url]

[url=http://java.sun.com/javase/6/docs/api/]Java?SE 6 JDK Javadocs[/url]

~

yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 3

On a side note...

calEx1.set(2007,04,01);

I recommend using the Calendar constants for the month to avoid confusion with Java's 0-indexed month numbering. I also recommend not using octal literals (the numbers beginning with zero). Example:

calEx1.set(2007,Calendar.MAY,1);

[url=http://java.sun.com/docs/books/tutorial/i18n/format/dateintro.html]The Java?Tutorial - Dates and Times[/url]

[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]

Best of luck!

~

yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 4

Sorry, i tried u r code but it throws,

- JDK 1.6 - Compile -

DateDiff.java:63: setTime(java.util.Date) in java.util.Calendar cannot be applied to (int)

fromServer.setTime((int)ex1.getDate());

^

Note: DateDiff.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

1 error

MCA134a at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 5

I can't see what's wrong. A few comments, though.

Calendar's months are 0-based, so 04 gives you the 5th month, i.e., May. If you wanted April, use Calendar.APRIL.

I see no point in creating a new Date object in Date dateObject = new Date();. Just use Date dateObject;.

Date.getDate() does return the day of month (an int, not a Date object). It's deprecated, though. You may use simply

fromServer.setTime(ex1);

OleVVa at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 6
> Sorry, i tried u r code but it throws,[EDIT] Oh, my bad. I didn't see that ex1 was a Date object. Just use:fromServer.setTime(ex1);~
yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 7

> > Sorry, i tried u r code but it throws,

>

> You didn't try my code; you injected an error. You

> tried to cast a Date object to an int. Get rid of the

> cast to int, and just try the code I posted.

>

> ~

I am afraid ex1 is a Date in OP's code. And the deprecated Date.getDate() method actually returns an int (the day of the month represented by this Date object.)

TimTheEnchantora at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 8

> > // here i am trying to set the date from that

> class

> >

> fromServer.set(ex1.getYear(),ex1.getMonth(),ex1.getDat

> e());

>

> The getDate() method returns a java.util.Date object,

> not a day of the month.

I was confused. To clarify, the getDate() method of Calendar returns a Date object. Where is the IllegalArgumentException?

~

yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 9
ok right, i made mistake.Thanks OleVV yawmark
MCA134a at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 10
> I was confused. To clarify, the getDate() method of> Calendar returns a Date object. Where is the> IllegalArgumentException?> > ~See reply#1, I guess.
TimTheEnchantora at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 11
> I am afraid ex1 is a Date in OP's code.Yeah, I caught that. Sorry for any confusion. See reply #6, I guess... ;o)
yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 12

> See reply#1, I guess.

Reply #1 doesn't say which line. If we assume that "format" points to a DateFormat object, the error is likely on the System.out.println() line, since format() takes a Date, not a Calendar. But the declaration of "format" isn't shown.

Where in reply #1 do you see the IllegalArgumentException?

yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 13

> Reply #1 doesn't say which line. If we assume that

> "format" points to a DateFormat object, the error is

> likely on the System.out.println() line, since

> format() takes a Date, not a Calendar.

It looked highly probable to me that format is a DateFormat.

I cannot find no other potential cause for an IllegalArgumentException to be thrown.

> But the declaration of "format" isn't shown.

Note that the fully qualified class names of Date and Calendar are not shown neither, everything is possible ;-P

TimTheEnchantora at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...
# 14

> I cannot find no other potential cause for an

> IllegalArgumentException to be thrown.

Me neither, but I thought I'd ask the OP for clarification.

> Note that the fully qualified class names of Date and

> Calendar are not shown neither, everything is possible

*rolls-eyes*

~

yawmarka at 2007-7-12 8:45:34 > top of Java-index,Java Essentials,Java Programming...