如何计算java中事件的经过时间? [英] How do I calculate the elapsed time of an event in java?

查看:159
本文介绍了如何计算java中事件的经过时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java访问系统时钟的简单/简单方法是什么,以便我可以计算事件的已用时间?

What's a simple/easy way to access the system clock using Java, so that I can calculate the elapsed time of an event?

推荐答案

我会避免使用 System.currentTimeMillis() 用于测量经过时间。 currentTimeMillis() 返回'挂钟'时间,这可能会改变(例如:夏令时,管理员用户更改时钟)并扭曲您的间隔测量值。

I would avoid using System.currentTimeMillis() for measuring elapsed time. currentTimeMillis() returns the 'wall-clock' time, which may change (eg: daylight savings, admin user changing the clock) and skew your interval measurements.

System.nanoTime() 返回自某个参考点以来的纳秒数(例如,JVM启动)因此,不会受到系统时钟变化的影响。

System.nanoTime(), on the other hand, returns the number of nanoseconds since 'some reference point' (eg, JVM start up), and would therefore not be susceptible to system clock changes.

这篇关于如何计算java中事件的经过时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆