Find the difference between two times in java

19,441

Instead of using Calendar it's easier to use System.currentTimeMillis():

def startTime() { 
    long startTime=System.currentTimeMillis();
    return  startTime;
}

Calculating time difference based on System.currentTimeMillis() is very common in Java, and you'll doing it right (I mean endTime - startTime)

So, your code could looks like:

long startTime = System.currentTimeMillis();
// .....
// processing request
// .....
long endTime = System.currentTimeMillis();
long differenceTime = endTime - startTime;
log.debug("Request time: " + differenceTime);
//or
log.debug("Request time: " + TimeUnit.MILLISECONDS.toSeconds(differenceTime) + " sec");
Share:
19,441
bharathi
Author by

bharathi

Updated on June 17, 2022

Comments

  • bharathi
    bharathi almost 2 years

    I have a search button, when the user clicks the search button the search() method is get called. I need to calculate the how much time it took to display the result to the user as we see in the google search.

    This is my code.

            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strDate;
    def startTime() { 
        Calendar cal = Calendar.getInstance();
        System.out.println("Current milliseconds since 13 Oct, 2008 are :"
        + cal.getTimeInMillis());
        long startTime=cal.getTimeInMillis();
        /*Date startNow = new Date();
        strDate = sdfDate.format(startNow);
        Date startTime=sdfDate.parse(strDate);
        print "startTime"+startTime*/
        return  startTime;
    
    }
    def endTime(){
        Calendar cal = Calendar.getInstance();
        System.out.println("Current milliseconds  :"
        + cal.getTimeInMillis());
        long endTime=cal.getTimeInMillis();
        /*Date endNow = new Date();
        print "endNow"+endNow
        strDate = sdfDate.format(endNow);
        Date endTime=sdfDate.parse(strDate);
        print "endTime"+endTime*/
        return endTime;
    }
    
    def differenceTime(long startTime,long endTime){
        print "requestEndTime"+endTime
        print "requestStartTime"+startTime
        long timeDifference = endTime - startTime;
        return timeDifference;
    }
    

    Here I am trying to get the starttime and endtime and trying to calculate the difference. I do know whether the way I implemented is right? Please tell me usually how the time difference is being calculated.

  • bharathi
    bharathi over 11 years
    How to check the difference between the starttime and endtime return in milliseconds is correct?
  • Mudasir Bhutto
    Mudasir Bhutto over 11 years
    If you want in seconds, then: long differenceTimeInSecs = (endTime - startTime)/1000;
  • Igor Artamonov
    Igor Artamonov over 11 years
    or TimeUnit.MILLISECONDS.toSeconds(endTime - startTime)
  • Basil Bourque
    Basil Bourque almost 8 years
    Incorrect answer. The Local… types lose any information about time zone, as that is their reason for being. That means you will get incorrect duration calculation when rolling over a change-over of Daylight Saving Time (DST) or other anomaly. For example, in Montréal this year, if your code ran for a minute at 2016-03-13T01:59:50, the result of your code would incorrectly be one hour and a minute as the clock "sprung forward" for DST from 2 AM to 3 AM.
  • Sanjit Kumar Mishra
    Sanjit Kumar Mishra almost 8 years
    I just answered the question asked. If you want to capture zonal details you can use ZonedDateTime.
  • Basil Bourque
    Basil Bourque almost 8 years
    But your code will fail, reporting incorrect results during anomalous moments such as DST cut-over. Say your first line captures the time 2016-03-13T01:59:50 in Montréal. The part you mark as //Code Here takes a full minute to run. During that minute the clock "springs forward" one hour with DST, jumping from 2 AM to 3 AM. Your 3rd line of code will capture the local date-time of 2016-03-13T03:01:50, the 3 AM hour (…T03:…) rather than 2 AM (…T02:…) as seen on most other days. Your code reports a duration of 1 hour + 1 minute (PT1H1M) rather than 1 minute (PT1M).
  • Sanjit Kumar Mishra
    Sanjit Kumar Mishra almost 8 years
    Ok, I understood what you are complaining. You can see the underline implementation of LocalDateTime in the JDK source to find how they are handling TimeZone. See the method Clock.systemDefaultZone()