简体   繁体   English

System.currentTimeMillis()是否具有日期的详细信息?

[英]Will System.currentTimeMillis() have the details of date?

I'm calculating the inactive interval of a particular user using the following code: 我正在使用以下代码来计算特定用户的非活动间隔:

inActiveInterval = System.currentTimeMillis() - objHttpSession.getLastAccessTime();

Will this give the correct difference to the last access time if the session is having yesterday's time? 如果会话有昨天的时间,这会为上次访问时间提供正确的区别吗?

Yes, this is correct. 是的,这是正确的。 According to the API of HttpSession.getLastAccessedTime() : 根据HttpSession.getLastAccessedTime()的API:

Returns the last time [...] as the number of milliseconds since midnight January 1, 1970 GMT 返回自格林尼治标准时间1970年1月1日午夜以来的最后一次毫秒数

Which is consistent with System.currentTimeMillis() : System.currentTimeMillis()一致:

Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. 返回值:当前时间与UTC 1970年1月1日午夜之间的差(以毫秒为单位)。

You can also use: 您还可以使用:

new Date().getTime()

as Date.getTime() has the same meaning: 由于Date.getTime()具有相同的含义:

Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date. 返回:自此日期以来,格林尼治标准时间1970年1月1日00:00:00以来的毫秒数。

Normally yes, the granularity of the time units usually depends on the OS, as explained by the System.currentTimeMillis() javadoc : 通常是的,时间单位的粒度通常取决于操作系统,如System.currentTimeMillis() javadoc所述

... the granularity of the value depends on the underlying operating system and may be larger. ...值的粒度取决于基础操作系统,并且可能更大。 For example, many operating systems measure time in units of tens of milliseconds. 例如,许多操作系统以几十毫秒为单位测量时间。

But that shouldn't pose a problem as the resolution of most OS's should be fine enough. 但这不应该带来问题,因为大多数操作系统的分辨率应该足够好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM