This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes System clock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System clock" Watch "System clock" New topic
Author

System clock

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I need to write a program that logs the time of input. It should use the system time, I'm guessing, but I'm unsure how to do this. I've been poking around the web a bit and have come across System.currentTimeMillis(), but if I understand correctly, it returns the number of milliseconds since January 1st, 1970? I'm not sure how to use this.

Can anyone give me a little hint, please?

Thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> the number of milliseconds since January 1st, 1970?

Close. The actual value is the number of millis since 00:00:00 GMT 1/1/1970. That's important because it's the same value no matter where you are on the planet, or off the planet. There is nothing magic about that reference point. Anything will do as long as we all agree to use the same reference.

You can store those values and reconstitute using a date class:

Date date = new Date( historicalSample );

Then you can print that using SimpleDateFormat, for example. There's also a System.nanoTime() for increased granularity.
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Thanks for the hints.

I came up with the following, which works, but gives a bit more info than I would like:


The first reply contained a succinct way of outputting the date, but I also need the time. Could that line be edited to include time in the output? Is it a simple matter of changing the date format to include time as well?



Thanks!
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Never mind, I think I got it. SimpleDateFormat("yyyy/MM/dd hh:mm:ss") gives me what I need, except it only does 12 hour format, so one in the afternoon shows as 01 rather than 13.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
except it only does 12 hour format


Use HH:mm:ss instead of hh:mm:ss. The javadocs of SimpleDateFormat list all the possible formatting options.
[ September 01, 2005: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
HH instead of hh will give you 0-23
"yyyy/MM/dd HH:mm:ss"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System clock