Find Differnce Time

I want to find the time difference in java so that I can create a new session if session expires else starts a new time.

0
source share
3 answers
Returns Time Diffrence in Seconds :




long elapsed_time = 0L;
    java.util.Date startTime = null;
    java.util.Date endTime = null;
    double fsec = 0L;
    String rSec = "";   

        startTime = new java.util.Date();

        <--------Some Operation----->

        endTime = new java.util.Date();
        elapsed_time = endTime.getTime() - startTime.getTime();
        fsec = (elapsed_time) / 1000.00;
        rSec = Double.toString(fsec);
        rSec = rSec + " Sec";
+3
source

You can use System.currentTimeMillis();to get the current system time in Java (in milliseconds from 01-01-1970 00:00:00 GMT).

The session object most likely also has a method to get the time when the last session was used (see the API documentation for any session object used).

Cross out the current time from now on from the session, and you know how much time has passed since the last session was used. If it is longer than the waiting period, do whatever you need.

, , . , HttpRequest.getSession() HttpSession, .

+3

Using Date means creating an object that is not needed because it just wraps System.currentTimeMillis(). Unfortunately, this function is only accurate to the millisecond and about 16 ms on some Windows systems.

The best approach is to use System.nanoTime()In Oracle JVM, Windows XP - 7, Solaris, and in recent versions of Linux, this is exactly less than 1 microsecond. He does not create any objects.

long start = System.nanoTime();
// do something.
long time = System.nanoTime() - start; // time in nano-seconds.
// time in seconds to three decimal places
String timeTaken = time/1000000/1e3 + " seconds"; 
+2
source

All Articles