Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference in time

Status
Not open for further replies.

amethystct

Programmer
Jan 26, 2004
21
FR
I'm getting the milli from 2 dates, current & a passed date. I want to know the difference in time down to the second between the 2 dates.

Given this date: 2004-02-05 18:25:00
and current of 2004-02-17 09:40:00
I get a time difference of 27:166:100. This is supposed to be hours min seconds.

This is how I'm calculating it:
long compare1Milli = compare1.getTime().getTime();
long compare2Milli = compare2.getTime().getTime();

// 1000 mil/sec
// 60 sec/min
//60 min/hour
//24 hour/day

long diffMillis = compare1Milli - compare2Milli;

long diffDays = diffMillis/
(24 * 60 * 60 * 1000);

long diffSecs = diffMillis/(1000);

// Get difference in minutes
long diffMins = diffMillis/(60*1000);

// Get difference in hours
long diffHours = diffMillis/(60*60*1000);

returnDifference = new Long(diffHours).toString() + ":" +
new Long(diffMins).toString() + ":" + new Long(diffSecs).toString();
 
I think there is a method 'getLong' or 'toLong' for date, giving the time in seconds or millis.

supposed it is in millis.

long l1 = compare1.toLong ();
long l2 = compare2.toLong ();

long ldiff = l2 - l1;

long secs = ldiff / 1000;
int s = (int) ldiff % 60; // get remaining seconds.
long mins = secs / 60;
int m = (int) mins % 60;
long hours = mins / 60;
int h = (int) hours % 24;
long days = hours / 24;
int d = (int) days;

return "Time: " + d + ":" + ....;


 
There's a gettimeinmilliseconds which is protected. getTime().getTime() returns the milliseconds.

long, int, Integer, it doesn't matter what I use the result is the same. Even if the time difference is only an hour the output isn't correct.

Using this as an example:
compare1: 1077614100712 compare2: 1077609600712
Compare1 is 02/24/2004 02:00:00
compare2 is 02/24/2004 03:15
This should return 1 hr, 15 minutes. It returns 1:75. So the hours are correct in this case, the minutes are 1 hr plus the minutes.

If the time is over 1 day then doesn't work at all. Is there a point where if the value is > ? I need to change the calculation or it's just to unreliable?
 
long now = System.currentTimeMillis();
long before = ... // time in *long* data type

int diffSecs = ((now - before) / 1000);
int diffMins = diffSecs / 60;

System.err.println("Time in seconds between the two is " +diffSecs +" or in minutes " +diffMins);
 
Here is the code I tested successfully.
Note that the Date-constructors are deprecated, and only used for rapid prototyping and testing.

Code:
import java.util.Date;

public class TimeDiff 
{
	public static void main (String args[])
	{
		Date compare1 = new Date ("02/24/2004 02:00:00");
		Date compare2 = new Date ("02/24/2004 03:15");
		long l1 = compare1.getTime ();
		long l2 = compare2.getTime ();
		
		long ldiff = l2 - l1;
		
		long secs = ldiff / 1000;
		int s = (int) (ldiff % 60); // get remaining seconds.
		long mins = secs / 60;
		int m = (int) (mins % 60);
		long hours = mins / 60;
		int h = (int) (hours % 24);
		long days = hours / 24;
		int d = (int) days;
		
		System.out.println ("Time 1:\t" + l1 + ":" + "....");
		System.out.println ("Time 2:\t" + l2 + ":" + "....");
		System.out.println ("long diff:\t" + ldiff);
		System.out.println ("secs diff:\t" + secs);
		System.out.println ("Time diff:\t" + d + " " + h + ":" + m + ":" + s);
	}
}
[code]
The result is 

Time diff: 0 1:15:0 

for the last line. (j2sdk1.4.2, linux)
 
P.S.: In my previous posting, I told you to use 'toLong', but you're correct, it has to be 'getTime ()'.
I only told you from my memory (which has a leak there) - but would you think, a method called 'getTime ()' returnes a time or a Long?
And how would you name a Method, which returns the time as long?
Of course 'toLong' 'asLong' or 'getLong'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top