• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Date class - GeekWatch

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Monika Balogh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help! I finished GeekWatch and very glad.
moni
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
But what is this mysterious "geekwatch" of which you speak?
 
reply
    Bookmark Topic Watch Topic
  • New Topic