# calculate difference of two times and express it in terms of hours

i need jsp code to find out the difference of two times in format hh:mm:ss and want to round off the result to a hour value.it's very urgent.plz someone help me

April 27, 2012 at 11:16 AM

```<%@page import="java.util.*"%>
<%@page import="java.text.*"%>
<%
try {
String date1 = "25/04/2012";
String time1 = "11:00 AM";
String date2 = "26/04/2012";
String time2 = "12:15 AM";

String format = "dd/MM/yyyy hh:mm a";

SimpleDateFormat sdf = new SimpleDateFormat(format);

Date dateObj1 = sdf.parse(date1 + " " + time1);
Date dateObj2 = sdf.parse(date2 + " " + time2);

long diff = dateObj2.getTime() - dateObj1.getTime();
double diffInHours = diff / ((double) 1000 * 60 * 60);
out.println(diffInHours+"<br>");
out.println("Hours " + (int)diffInHours+"<br>");
out.println("Minutes " + (diffInHours - (int)diffInHours)*60 );

} catch (ParseException e) {
e.printStackTrace();
}
}
}
```

The above code parse two times and ouputs the difference between two times in terms of hours.

Related Tutorials/Questions & Answers:
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss...(); } } } The above code parse two times and ouputs the difference between two times in terms
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss...(); } } } The above code parse two times and ouputs the difference between two times in terms
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss...(); } } } The above code parse two times and ouputs the difference between two times in terms
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss...); out.println(diffInHours+"<br>"); out.println("Hours " + (int
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss...); out.println(diffInHours+"<br>"); out.println("Hours " + (int
calculate difference between two dates
calculate difference between two dates  hi, I was actually working on to calculate the number of days between two dates of dd/mm/yyyy format... the difference between the two dates
calculate difference between two time in jsp
calculate difference between two time in jsp  How to calculate difference between two dates
How to calculate attending hours?
How to calculate attending hours?  I need to calculate attending hours of a employee in a day in my project to make their monthly salary . By using work starting time and ending time. How should I do it using Date class? Or any
How to calculate Date Difference in Java?
How to calculate Date Difference in Java?  Hi, I have two dates variable. How to calculate Date Difference in Java? Thanks   Hi, You can use the Calendar class for calculating the date difference in Java. Get data
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ..., '01/02/2012', 103))   DATEDIFF() in MYSQL always returns the difference
Date Difference
Date Difference       This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours
Difference in two dates - Java Beginners
Difference in two dates  Hello there once again........ Dear Sir... on that. The thing is, that I need to find the difference between the two dates in JAVA... the difference between two dates. public class DateDiffDemo { public static
Javascript calculate number of days between two dates
Javascript calculate number of days between two dates In this tutorial, you will learn how to calculate number of days between two dates.For this, you need... the later one from the earlier one returns the difference in milliseconds
Hi .Difference between two Dates - Java Beginners
Hi .Difference between two Dates  Hi Friend.... Thanks for ur Very good response.. Can u plz guide me the following Program.... difference between two dates.. I need to display the number of days by Each Month
This code send email two times but i want only once
This code send email two times but i want only once   public void dbbackup_notify(String email,String data,String subject){ String toEmails = email; Session session=Session.getInstance(props, new
This code send email two times but i want only once
This code send email two times but i want only once   public void dbbackup_notify(String email,String data,String subject){ String toEmails = email; Session session=Session.getInstance(props, new
How to get of lastmodified file list between two times in java?
How to get of lastmodified file list between two times in java?  Hi, here is a code that list the last modified files in a directory between two dates. This is working well but is it possible to do the same thing between two
how to calculate addition of two distances in feets and inches using objects as functions arguments
how to calculate addition of two distances in feets and inches using objects as functions arguments  how to calculate addition of two distances in feets and inches using objects as functions arguments in java
Q, 12 WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
Q, 12 WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java  Q, 12 WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java  WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java  WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java  WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
Java Swing Date Difference
Java Swing Date Difference In this tutorial, you will learn how to find the difference between two dates. Here is an example that accepts two dates from... the following method that calculates the date difference in terms of days
what is difference between one-way data binding and two-way data binding?
what is difference between one-way data binding and two-way data binding?  what is difference between one-way data binding and two-way data binding? Thanks
method and function difference
method and function difference  so far, I understand that method and function are the same thing... but is there any difference between the two terms? Please explain me if both are different terms. Thanks in advance
difference
difference  difference between hashtable and hashtree
difference
difference  what's the difference between mysql and sql
difference
difference  difference b/w java and asp.net
Total Hours Calculation.
Total Hours Calculation.   Hi,How to calculate the total hours by startime and endtime.not railway time just only normal time.please help. Thanks
Difference
Difference  What Is the difference between JDK & SDK
difference
difference    what is the difference between the JDBC-ODBC bridge, the Native-API-Partly-Java driver, and the JDBC-Net-All-Java Driver is the placement of the database access libraries   Please visit the following link