I want to find time difference in java so that I can create new session if session expires else will asign new time.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can use
System.currentTimeMillis();
to get the current system time in Java (in milliseconds since 01-01-1970 00:00:00 GMT).The session object most likely also has a method to get the time when the session was last used (look it up in the API documentation of whatever session object you're using).
Subtract the current time from that time from the session and you know how long it has been since the session was last used. If it is longer than the timeout period, do whatever you have to do.
Note that servlet containers have a built-in mechanism for invalidating sessions, you don't need to invalidate sessions manually. Also,
HttpRequest.getSession()
will automatically create a newHttpSession
object for you if there is no session.Using Date means creating an object which isn't need as it just wraps
System.currentTimeMillis()
Unfortunately this function is only accurate to a milli-second at best and about 16 ms on some windows systems.A better approach is to use
System.nanoTime()
On Oracle's JVM, on Windows XP - 7, Solaris and on recent versions of Linux, this is accurate to better than 1 micro-second. It doesn't create any objects.