Hi Monika
Have a look at the following code:
<code>
<pre>
import java.util.*;
public class DateTest
{
public static void main(
String[] args)
{
// 2 different ways to set up a date for 'now'
Date now = new Date( System.currentTimeMillis() );
Date now2= new Date();
// how to set up a date for 1st Jan 1971 (the 'base' date)
Date base= new Date(0);
// have a look at what we have made
System.out.println("now: " + now.toString() );
System.out.println("now2: " + now2.toString() );
System.out.println("base: " + base.toString() );
// what is the difference in milliseconds between the 2
long baseMillis= base.getTime();
long nowMillis= now.getTime();
System.out.println( "Time in ms since base: " + (nowMillis - baseMillis) );
}
}
</pre>
</code>
Calling <code>getTime()</code> on a java.util.Date object does not return a date object, it returns a long representing the number of milliseconds between that date and the base date in 1971.
You are using:
<code>
Date date = new Date();
</code>
which creates a date representing 'now'. So you already have one of the 2 objects you need for the comparison. You then need to create a date for the base date. The Date constructor is overloaded, there is a version that takes a long representing the number of milliseconds since the base date. Hence, if you use that constructor with an argument of zero, the resultant Date object will represent Jan 01 1971 (the base date).
You need 2 objects for the comparison - you only seem to be creating one. You don't need to use <code>setTime()</code>, unless you plan on doing the operation with just one Date object, which is easily possible, but fairly pointless.
Once you have the 2 objects, simple arithmetic comparison on the results of calling <code>getTime()</code> on each gives you what you need.
In fact, we already know that calling getTime on the 'base' Date object will return 0, since we passed 0 to the constructor. This is why I originally said you could use getTime on the date object representing now, but hopefully this makes the relevant constructors and methods of the Date class a little clearer.
Is that helpful?
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
[This message has been edited by Michael Fitzmaurice (edited September 12, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 12, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 12, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 12, 2001).]