Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Calculating time between two date objects

Thread view: 
Brian - 09 Dec 2007 20:21 GMT
Hi

I need a few lines of code that calculate the time between two Date()
objects.

I need it to calculate the time between two occurences - if there is a
better solution than using Date() objects I'm all ears :)

Sincerely
Brian
Arne Vajhøj - 09 Dec 2007 20:26 GMT
> I need a few lines of code that calculate the time between two Date()
> objects.
>
> I need it to calculate the time between two occurences - if there is a
> better solution than using Date() objects I'm all ears :)

.getTime() on both and subtract ?

Arne
Daniel Pitts - 09 Dec 2007 21:47 GMT
>> I need a few lines of code that calculate the time between two Date()
>> objects.
[quoted text clipped - 4 lines]
>
> Arne
You can also use System.currentTimeMilles(), depending on what you need.
   How close together are the occurrences? Is this for timing a short
method, or is it for days apart?

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Zig - 09 Dec 2007 21:54 GMT
> Hi
>
> I need a few lines of code that calculate the time between two Date()
> objects.

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* Computes the time difference between two dates, as a-b.
* @param units the desired units of the result. Units larger than
*   MILLISECONDS may cause truncation
*/
public long getDifference(Date a, Date b, TimeUnit units) {
  return units.convert(a.getTime()-b.getTime(), TimeUnit.MILLISECONDS);
}

> I need it to calculate the time between two occurences - if there is a
> better solution than using Date() objects I'm all ears :)

Date is appropriate if you are echoing the Date information to a log,  
console, or are otherwise presenting the "time of occurence" somewhere in  
a human readable format. It uses System.currentTimeMillis under the hood.

System.currentTimeMillis is appropriate for exchanging time between two  
remote machines, or for saving a timestamp to be read back in a later  
session. But, for 2 or more machines, keep in mind that those machines may  
have the clock set slightly differently. System.currentTimeMillis is also  
dependant on the OS clock granularity, and on Windows this value only  
changes every 10ms or so. Lastly, if the system clock changes between  
calculations, then you don't get a measurement of the timelapse between  
said events. While it may be uncommon for a user to change the clock  
between events, many modern systems do have the option for network time  
syncs, which can automatically adjust the clock in the background.

System.nanoTime is appropriate for measuring elapsed time within a single  
VM session. If you just call System.nanoTime(), the result doesn't have  
any meaning that you can display. But differences between calls will give  
you a good measure of elapsed time. Of course, differences between  
System.nanoTime taken on one host vs another host aren't going to have  
much value or meaning, and the same goes for taking differences between  
the current VM session and a previous session. However, System.nanoTime is  
immune from clock changes, and will give you correct elapsed time results,  
and with much greater precision than System.currentTimeMillis.

Hope that helps to answer your question.

-Zig
Roedy Green - 10 Dec 2007 11:42 GMT
>I need a few lines of code that calculate the time between two Date()
>objects.

see http://mindprod.com/jgloss/time.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.