skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, October 4, 2012

Time Difference in Hours Minutes Seconds

Posted by Admin at 12:40 PM – 0 comments
 
This code will calculate the time difference.
We need to pass the two date objects which represents the Start and End time.
It will return an array which in turn contain the difference details in Hours,Minutes,Seconds in Array Index 0,1 and 2 respectively.

public Long[] calculateExecutionTime(java.util.Date startTime, java.util.Date endTime) {

  Long timeDetailsArray[] = new Long[3];

  long difference = endTime.getTime() - startTime.getTime();

  long hours, minutes, timeInSeconds;

  long seconds;

  difference = difference / 1000;

  timeInSeconds = difference;

  hours = timeInSeconds / 3600;

  timeInSeconds = timeInSeconds - (hours * 3600);

  minutes = timeInSeconds / 60;

  timeInSeconds = timeInSeconds - (minutes * 60);

  seconds = timeInSeconds;

  timeDetailsArray[0] = hours;
  timeDetailsArray[1] = minutes;
  timeDetailsArray[2] = seconds;
                                          
                            System.out.println( timeDetailsArray[0] +" Hour(s) " + timeDetailsArray[1] +" Minute(s) "+ timeDetailsArray[2] + "Seconds ");

  return timeDetailsArray;
 }

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger