jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Date & Time Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date & Time Problem" Watch "Date & Time Problem" New topic

Date & Time Problem

safraz hanas

Joined: Feb 21, 2007
Posts: 10
Hi to all, I'm having a buring problem

Following code snippet i got from here, I use to explain my problem.

1. Date d = new Date();
2. System.out.println(d);
3. Calendar cal = new GregorianCalendar();
4. cal.setTime(d);
5. cal.roll(Calendar.HOUR_OF_DAY, -1); // problem is here
6. d = cal.getTime();
7. System.out.println(d);

lets say, in line no 2 printed as "Wed Feb 21 19:23:58 IST 2007"
& line 6 printed "Wed Feb 21 18:26:15 IST 2007"
if i pass 20 instead of 1 in line no 5, I'm getting "Wed Feb 21 23:27:16 IST 2007"

This is not what i need, i should get something like "Wed Feb 20 ......"

How can i solve this???

Guys, I need your help!!!
Thanks in advance
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37874
Welcome to the Ranch.

Please don't post the same question twice; delete the other copy.

Go and check what the "roll" method does. It doesn't turn the clock back or forward.
This is what the API (look for GregorianCalendar -> roll) says:-
Adds a signed amount to the specified calendar field without changing larger fields. A negative roll amount means to subtract from field without changing larger fields. If the specified amount is 0, this method performs nothing.
So from that description, rolling -20 (not 20 as you say) to HOUR will change 12.34.56 21st February to 16.34.56 21st February.

Yes, it is difficult to understand; in fact a lot of people think Java's handling of dates is difficult to understand throughout.
safraz hanas

Joined: Feb 21, 2007
Posts: 10
Thanks for the early reply

Ok, I understand it. Do you have any suggetion for this. I mean any other alternatives
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11143

i deleted the duplicate of this thread.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37874
You could try something like:-
That won't work at all well if you try to roll 48 hours. I am not sure I have used the right terms for DAY or HOUR.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37874
There may be other ways of rolling back.

You know the Calendar types are actually a timestamp which count milliseconds since 1st January 1970 at midnight. [It was actually 1.00am because we had summer time all winter that year.] Convert your Calendar object to milliseconds, convert your hour to milliseconds (3600000 of them), add the two values, and convert the milliseconds back.
Not sure of the full details. You will have to see what you can find in the API.
subject: Date & Time Problem
Similar Threads
Get N-Days before today using Date
Java util Date & Calendar - 1900 years
new Date and currentTimeMillis
My way for multi-client failed?
jk_mod issue