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");
Author by
bharathi
Updated on June 17, 2022Comments
-
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 over 11 yearsHow to check the difference between the starttime and endtime return in milliseconds is correct?
-
Mudasir Bhutto over 11 yearsIf you want in seconds, then: long differenceTimeInSecs = (endTime - startTime)/1000;
-
Igor Artamonov over 11 yearsor
TimeUnit.MILLISECONDS.toSeconds(endTime - startTime)
-
Basil Bourque almost 8 yearsIncorrect 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 almost 8 yearsI just answered the question asked. If you want to capture zonal details you can use ZonedDateTime.
-
Basil Bourque almost 8 yearsBut 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 of2016-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 almost 8 yearsOk, 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()