Student needs help with Date() in Windows.

Hi all. I am newbie to Java and try a test:

import javax.swing.*; // for JFrame and JOptionPane

import java.util.*;// for Date

import java.text.*;// for SimpleDateFormat

class MyDate {

static public void main (String[] args) {

JFrame mywindow;

mywindow = new JFrame();

mywindow.setSize(500,500);

mywindow.setTitle("Using the SimpleDateFormat Class ");

mywindow.setVisible(true);

Date now = new Date();

//SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy");

SimpleDateFormat sdf = new SimpleDateFormat();

JOptionPane.showMessageDialog(mywindow, "Today is " + sdf.format(now));

}

}

It works on a Linux machine, but gives me this error in Windows (two different windows machines).

init:

deps-jar:

compile-single:

run-single:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date

at java.text.DateFormat.format(DateFormat.java:281)

at java.text.Format.format(Format.java:140)

at MyDate.main(MyDate.java:20)

Since I am a student, I really would appreciate any help as soon as possible. I get graded on this Monday.

thx, Tom

[1252 byte] By [tashworta] at [2007-11-27 6:46:23]
# 1
I believe the Date class is dead (deprecated). Try your look with the Calendar class. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 2
That code works totally fine on my computer (windows).
TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 3
It works OK when I try it with WinXP SP2. Check that you don't have a stray Date class in the default package, one that you've written.
pbrockway2a at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 4

Could I have some configuration issue? I just ran

c:\java mydate

and got

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date

at java.text.DateFormat.format(DateFormat.java:281)

at java.text.Format.format(Format.java:140)

at MyDate.main(MyDate.java:20)

tashworta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 5
> I believe the Date class is dead (deprecated). Date: I'm not dead yet! Much of Date is deprecated, but not the whole class.
Hippolytea at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 6
No I just downloaded the new one with NetBeans. I am running Java version 1.6.0_01 I downloaded this to two of my Windows XP Systems.Tom
tashworta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 7
change this line:Date now = new Date();tojava.util.Date now = new java.util.Date();and come back with the errors. also java is case sensitive (but im sure you typed) java MyDate != mydate
TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 8
The java.util.Date now = new java.util.Date(); works, but now I am confused. I have no clue why the other code worked on Linux, but not on Windows.Thanks for the fix in Windows, but do you have a clue why it worked in Linux the other way.Very confused newbie here....
tashworta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 9
read pbrockway2's comment. you must have a polluted namespace.(some other class is named Date and the compiler doesnt know what you intend)
TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 10

>> Very confused newbie here....

ah-ha. the "newbie" comment knocked over some domino's in my brain:

what is your classpath setting?

new developers tend to do strange classpath related things like

putting their code in the java system folders or throwing every

project theyve ever written into one folder.

TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 11
you can add the line System.out.println(Date.class);to find which Date is the problem (without the java.util)....Message was edited by: S_i_m_u
S_i_m_ua at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 12
Geez, you guys this is the best and most helpful forum I have ever been to. I found the problem.I have a file named Date.java in my src folder in the Netbeans project file. That translated to a rouge class. I deleted the file and all runs well.thx Tom
tashworta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 13
> That translated to a rouge class. Not a red herring? ;-)
Hippolytea at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 14
>> Geez, you guys this is the best and most helpful forum I have ever been to. youre a good egg. we're here for you.
TuringPesta at 2007-7-12 18:18:53 > top of Java-index,Java Essentials,Java Programming...
# 15
> > That translated to a rouge class. > > Not a red herring? ;-)I think you're reading this thread with rose-colored glasses.~
yawmarka at 2007-7-21 22:05:42 > top of Java-index,Java Essentials,Java Programming...