How to get system time in Java without creating a new Date

前端 未结 4 1758
抹茶落季
抹茶落季 2020-12-28 12:36

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)

4条回答
  •  爱一瞬间的悲伤
    2020-12-28 12:58

    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.

提交回复
热议问题