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.
>> 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
posted
0
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
posted
0
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.
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 ]