• 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 Computation

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I know this is very basic in Java. But if you guys don't mind I would like to add how to compute the difference of two given dates(For getting the age).

In PHP, you can just specify (aDate - today). I'm just a little curious if you could just subtract 2 Date objects directly and have the same effect? Or am I just hallucinating? Thanks!
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends how precise you want the measurements. My favorite way in finding the difference between two dates is to convert them to long values (milliseconds since the epoch) then subtract them such as:


Take a look at http://javaalmanac.com/egs/java.util/CompDates.html for more examples.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on what you mean by "difference between dates" you have to be careful. For example, if you start with the difference in two dates long timestamps, you may be missing the distinction between two longs timestamps that occur 10 hours apart on the same day and two long timestamps that occur 10 hours apart on consecutive days. And I haven't even mentioned dealing with leap years!

To find durations in days, I start with two calendar objects (in the same time zone!) and roll them both back to midnight, then extract long time stamps and go from there. To calculate age in years, I extract the year, month and day from the calendar objects and take it from there.

If you work with dates you may want to check out joda time.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i just developed some financial methods where the date differences were really important. after playing around (and freaking out) with java standard packages, i tried joda time, and solved my problem within minutes...

good luck,
jan
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic