Solved

Adding/Subtracting dates to get TimeSpan

Posted on 1998-07-24
2
219 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now