Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calculating time between two date objects

Reply
Thread Tools

Calculating time between two date objects

 
 
Brian
Guest
Posts: n/a
 
      12-09-2007
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      12-09-2007
Brian wrote:
> 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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      12-09-2007
Arne Vajh°j wrote:
> Brian wrote:
>> 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

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?

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Zig
Guest
Posts: n/a
 
      12-09-2007
On Sun, 09 Dec 2007 15:21:11 -0500, Brian <mail@DEL_oddball_ETE.dk> wrote:

> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-10-2007
On Sun, 09 Dec 2007 21:21:11 +0100, Brian <mail@DEL_oddball_ETE.dk>
wrote, quoted or indirectly quoted someone who said :

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


see http://mindprod.com/jgloss/time.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating number of days between two dates. clintonb C++ 9 05-23-2007 09:32 AM
Calculating time of employee session from the log date/time stampusing perl Jayesh Kamdar Perl Misc 2 05-25-2005 12:08 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
asp.net date objects vs. classic asp date objects J. Muenchbourg ASP General 1 10-03-2003 06:06 PM
Help! I am having problems with calculating the total seconds from two date/times. Tim ASP General 3 08-05-2003 07:45 PM



Advertisments