aspose file tools*
The moose likes Beginning Java and the fly likes Date class - GeekWatch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date class - GeekWatch" Watch "Date class - GeekWatch" New topic
Author

Date class - GeekWatch

Monika Balogh
Greenhorn

Joined: Aug 28, 2001
Posts: 26
Hi,
Can somebody help me out with some examples how to use this class? I just don't get how I can calculate the milliseconds from 1970. I could not find an appropriate method to calculte that. I have an object to represent a Date object that is the first day of 1970 and I have one for just now as well. My guess would be to perform some sort of arithmetic on these object but I don't understand how can that be done.
Any help would great.
Thanks,
moni

Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Moni
The simple way to find out the number of milliseconds since Jan 1st 1970 is to call <code>getTime</code> on the Date object that represents 'now'.
I have copied the following directly from the Java 2 API documentation:
public long getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
Hope this helps
P.S. Does this qualify me as a 'Geek' ?

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Monika Balogh
Greenhorn

Joined: Aug 28, 2001
Posts: 26
Hi Michael,
I have looked at this method and when I call it on my date object it just prints out the date at 1970. I did something like this:
The output is:
Thu Jan 01 00:00:00 GMT 1970

I thought that if I use the getTime method I will get a date object back for different operations to work out the seconds and so on. But the other problem is that I cannot use arithmetic on a date object. I am confussed. Did this make sense? Any help would be great.
Thanks,
moni

P.s:
By the way what is wrong with being a geek? Be proud!
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
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).]
Monika Balogh
Greenhorn

Joined: Aug 28, 2001
Posts: 26
Thank you so much for your help! I finished GeekWatch and very glad.
moni
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
You're welcome.
But what is this mysterious "geekwatch" of which you speak?
 
 
subject: Date class - GeekWatch