Solved

Adding/Subtracting dates to get TimeSpan

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 121
bitbucket vs gitbucket 3 55
Java passing arguments (type error) 2 46
Need to track down Infection in a Server 2008 domain user profile 7 48
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

947 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

22 Experts available now in Live!

Get 1:1 Help Now