?
Solved

Adding/Subtracting dates to get TimeSpan

Posted on 1998-07-24
2
Medium Priority
?
250 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 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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