You can use the method getLastAccessedTime()
that returns the time (in milliseconds) of the last session-associated request.
And then use the method getMaxInactiveInterval()
to obtain the maximum time interval (in seconds) that the container will keep this session open.
Having these two values, simply subtract the session time by the time of the last request, remembering that one method returns the value in milliseconds while the other returns in seconds:
Long segundos = session.getMaxInactiveInterval() -
(System.currentTimeMillis() - session.getLastAccessedTime()) / 1000;
The conversion, for minutes for example, can be done by means of an object TimeUnit
:
long minutosRestantes = TimeUnit.SECONDS.toMinutes(segundos);
out.print(minutosRestantes); // Exibe o tempo (em minutos) restante