I need to get the system date, and Java provides the new Date().getTime()
.
But I need to avoid new object allocation (I'm working on a embedded system). How can I get the system time without allocating a new Date
object?
This question is related to
java
date
memory-management
This should work:
System.currentTimeMillis();
Use System.currentTimeMillis() or System.nanoTime().
You can use System.currentTimeMillis()
.
At least in OpenJDK, Date
uses this under the covers.
The call in System
is to a native JVM method, so we can't say for sure there's no allocation happening under the covers, though it seems unlikely here.
Source: Stackoverflow.com