java tick_java中的Environment.tickcount

正如@TedHopp所提到的,一种可能性是使用System.currentTimeMillis()。在我的情况下,我想要一个“滴答计数”,以秒为单位,而不是毫秒。这是我目前用于相应C#方法的Java版本的内容。

// Static field used by the tickCountInSeconds() method

private static long _firstCallTimeSeconds = 0;...

/**

* Method to get an arbitrary constantly increasing time in seconds, i.e., a time in seconds that

* can be used to compare the relative times of two events, but without having any other meaning.

*

* The .Net version of this method uses the Windows "tick count" facility, but since that doesn't

* exist in Java we fake it by getting the system (Unix-style) time in milliseconds and

* converting it to seconds. But to avoid the "year 2038 problem" (or to avoid criticism for

* creating a year 2038 vulnerability) the time of the first call is saved in a static field and

* subtracted from the returned result.

*/

private synchronized static int tickCountInSeconds() {

long currentTimeSeconds = System.currentTimeMillis() / 1000L;

if (_firstCallTimeSeconds == 0) {

_firstCallTimeSeconds = currentTimeSeconds;

}

return (int)(currentTimeSeconds - _firstCallTimeSeconds);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值