Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java getting current date without creating a new Date object

Tags:

java

I have a game that loops. I need to check the current number of milliseconds. I don't want to create a new Date object just to get the number of milliseconds that has passed. Is there a way to get the current time without creating a new Date object every iteration of my game loop?

Example:

Date d = new Date();

while(true)
{
    long currentTime = d.getCurrentTime();
}

In the above code, the value of the currentTime variable would continuously change.

like image 787
Matthew Avatar asked Jul 03 '26 22:07

Matthew


1 Answers

Rather than using Date, you can access the static method

long currentTime = System.currentTimeMillis()

This returns the current time of the given system in milliseconds.

like image 128
alpha_ulrich Avatar answered Jul 06 '26 11:07

alpha_ulrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!