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
Solved

Adding/Subtracting dates to get TimeSpan

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 46
ejb stateless example 2 23
Bot application - advice 3 38
Eclipse Java import and method not resolved 4 48
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

856 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