• 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/Calendar computations

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ms. Jennifer Wallace, I hear you on that one...
I'm trying to figure out how to add 1 hour to the current Date for later comparison. But seeing as how a load of functions in the Date class are deprecated and the API points to the Calendar class I'm getting confused.
I simply want to create a Date object and add 1 hour to it. Any ideas? Hahaha, sounds simple
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came up with this but had to use GregorianCalendar because Calendar is abstract. Anything simpler? I would simply like to bypass that whole Calendar stuff but I don't know if that's possible.
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.HOUR, +1);
Date plusOneHour = cal.getTime();
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure:

The GregorianCalendar is useful if you need to work with things like how many days are in a month, or when leap years are, or whether it's Tuesday, etc - but for many simpler things it's easier to just do the calculation yourself in milliseconds.
Note that for some odd, unknown reason, Date is not immutable - even though it's otherwise very much like a wrapper class around a long value representing time in milliseconds. Typically this means you'll want to clone() it or otherwise create a new instance before you return it from a public function - otherwise other programmers can confuse the heck out of you by changing your Date without you realizing it.
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waaaay easier. Thanks Jim
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For comparison, the Calendar solution:

Not that bad - but cetainly longer than it needs to be. On the other hand, if you need something like "noon of the next business day", Calendar's complexities become much more useful.
reply
    Bookmark Topic Watch Topic
  • New Topic