简体   繁体   中英

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() :

Returns the last time [...] as the number of milliseconds since midnight January 1, 1970 GMT

Which is consistent with System.currentTimeMillis() :

Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

You can also use:

new Date().getTime()

as Date.getTime() has the same meaning:

Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

Normally yes, the granularity of the time units usually depends on the OS, as explained by the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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