Calculate date difference

Posted on 2004-10-25
Medium Priority
Last Modified: 2008-01-09
I have two fields in the same SQL 2K server. The fields are identified as arrival_time and departure_time. The fields are date/time fields and contain information as such:

Arrival_Time                         Departure_Time
10/20/2004 08:00:00 AM       10/24/2004 04:00:00 PM

How can I calulate the difference in this information to display the difference in hours?
Question by:rcmb
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 12402124
(Partially pesudo-code)

Timestamp arr = <select arrival>;
Timestamp dep = <select departure>;

long diff = arr.getTime() - dep.getTime();
int hourDiff = (int)diff / 1000 / 60 / 60;
LVL 13

Expert Comment

ID: 12402165
some thing like that:

public static float getTimeElapsedInHours(Date laterTime, Date earlierTime)
      long l1 = laterTime.getTime();
      long l2 = earlierTime.getTime();
      long difference = l1 - l2;

  return ( ( (float) difference ) / (1000.0f * 60.0f * 60.0f)  );
LVL 12

Author Comment

ID: 12402744
Works great.

Thanks again,
LVL 86

Expert Comment

ID: 12402780


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month4 days, 10 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question