Getting 'running' current time

O

ohaya

Hi,

I'm using:

import java.util.Date;
..
..
Date now = new Date();

to get the current date/time. This works fine, but I was wondering:
Each time this is invoked, it is doing a "new Date()", i.e., creating a
new Date instance.

Is there any way I can just refresh the current time, instead of doing a
"new" each time? Something like "now.update();"?

Thanks,
Jim
 
S

Steve W. Jackson

Hi,

I'm using:

import java.util.Date;
.
.
Date now = new Date();

to get the current date/time. This works fine, but I was wondering:
Each time this is invoked, it is doing a "new Date()", i.e., creating a
new Date instance.

Is there any way I can just refresh the current time, instead of doing a
"new" each time? Something like "now.update();"?

Thanks,
Jim

To capture the current date/time, use System.currentTimeMillis(), which
returns the current time in milliseconds since the epoch (a long). To
get a later date/time, call it again. You can pass that to the Date
constructor if you wish. Or if you want to create a Date object for
"now" (as in your example) and then update it later, you can call its
setTime(long time) method with the then-current system time:

now.setTime(System.currentTimeMillis());

You should get comfortable with the API JavaDocs, BTW.

= Steve =
 
E

Eric Sosman

ohaya said:
Hi,

I'm using:

import java.util.Date;
.
.
Date now = new Date();

to get the current date/time. This works fine, but I was wondering:
Each time this is invoked, it is doing a "new Date()", i.e., creating a
new Date instance.

Is there any way I can just refresh the current time, instead of doing a
"new" each time? Something like "now.update();"?

now.setTime(System.currentTimeMillis())
 
O

ohaya

Eric said:
now.setTime(System.currentTimeMillis())


Eric and Steve,

Thanks!

Sorry. I did look at the Javadocs, but I missed the "currentTimeMillis"
part...

Jim
 
R

Roedy Green

to get the current date/time. This works fine, but I was wondering:
Each time this is invoked, it is doing a "new Date()", i.e., creating a
new Date instance.

Is there any way I can just refresh the current time, instead of doing a
"new" each time? Something like "now.update();"?

You are correct that works but creates a useless object in the
process.

See http://mindprod.com/jgloss/time.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top