views:

149

answers:

1

I am developing a J2ME application (CLDC 1.1 and MIDP 2.0) and I was wondering, What is the best way to get the time span between 2 dates?

thanks,

Tal.

Edit:

Here is a little sample using the answer below:

public class TimeHelper {
public static long getTimeSpanInMilliSeconds(Date d1,Date d2) {
    return Math.abs(d1.getTime() - d2.getTime());
}

public static double getTimeSpanInMinutes(Date d1,Date d2) {
    return getTimeSpanInMilliSeconds(d1, d2) / 60000;
}

}

A: 

This is not so easy to answer in general. Do you want the time span in seconds? In this case you could do the following:

Calendar c1 = ...;
Calendar c2 = ...;
long deltaSeconds = (c2.getTime().getTime()-c1.getTime().getTime())/1000;
Wangnick
That difference is in milliseconds, not in seconds (http://java.sun.com/javame/reference/apis/jsr118/java/util/Date.html#getTime%28%29).
Jala
converting the milliseconds into minutes (which was what I was looking for) isn't hard at all :) (dividing by 60,000)thanks!
Tal