File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dates" Watch "Dates" New topic


Tim Eapen

Joined: May 28, 2006
Posts: 22
Hello everybody:

I am working through the Sierra/Bates SCJP book. Currently I am studying dates and have encountered something that is really bugging me. Here is some very simple code:

Here is the output for my JVM:

Notice that when I added 6 seconds, the hour field was rolled back two hours. I expect the last line to be: Date: Fri Feb 28 21:00:06 EST 1969.

Why does rolling the seconds field seem to affect the hour field? The JavaDoc states the following for the roll method:

Adds the specified (signed) amount to the specified calendar field without changing larger fields.

Since hour is a larger field than second, I expect rolling the seconds field to have no impact on the hour field. Am I missing something here?

vitthal bywar

Joined: Aug 05, 2003
Posts: 1
This works fine on my JVM.
I tried
Calendar cal = Calendar.getInstance();
Date date = new Date(1);
System.out.println("Date1" + cal.getTime());
cal.roll(Calendar.MONTH, 2);
System.out.println("Date2" + cal.getTime());
cal.roll(Calendar.HOUR, 2);
System.out.println("Date3" + cal.getTime());
cal.roll(Calendar.SECOND, 6);
System.out.println("Date4" + cal.getTime());

And output was
Date1Thu Jan 01 05:30:00 IST 1970
Date2Sun Mar 01 05:30:00 IST 1970
Date3Sun Mar 01 07:30:00 IST 1970
Date4Sun Mar 01 07:30:06 IST 1970
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Doesn't work on mine. strange stuff.

Tim Eapen

Joined: May 28, 2006
Posts: 22
This is bizarre - I get the expected output at work. On my workstation I have Java 1.4.2. At home I have Java 1.5. Could that be the issue?

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

I see this bug with JDK1.5 if I compile with -source 1.4 or 1.5


all events occur in real time
I agree. Here's the link:
subject: Dates
It's not a secret anymore!