• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date & Time Problem

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the early reply

Ok, I understand it. Do you have any suggetion for this. I mean any other alternatives
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i deleted the duplicate of this thread.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may be other ways of rolling back.

Maybe:-
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.
reply
    Bookmark Topic Watch Topic
  • New Topic