Solved

Adding/Subtracting dates to get TimeSpan

Posted on 1998-07-24
2
246 Views
Last Modified: 2010-04-16
Is there a Java class that has methods useful for Adding/Subtracting dates to get a Timespan between the dates?  For example, I want to know how many years/months/days will have elapsed between my birthday and 1/1/2000.  I really don't want to have to write the code myself, if there is a class method that will handle it for me.
0
Comment
Question by:kmullin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1228134
There is no class that does exactly that. However, for rounded answers, it is relatively straightforward:

For years, use the difference: targetyear-otheryear

For months, use year difference * 12 + (targetmonth-othermonth)

For days, the tricky one, you can get the UTC from the Date class. It returns a millisecond count from some point (1900 I believe). Get the UTC for both dates (set for the same time, say midnight), then divide by the number of milliseconds in a day:
(60 * 60 * 24 * 1000)

0
 
LVL 16

Expert Comment

by:imladris
ID: 1228135
Sorry, that should have been, get the UTC for both dates (set for the same time, say midnight), get the difference between the two, and THEN divide by the number of milliseconds in a day:
(60 * 60 * 24 * 1000)

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question