Solved

Calculate date difference

Posted on 2004-10-25
2,469 Views
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?
0
Question by:rcmb
    5 Comments
     
    LVL 86

    Accepted Solution

    by:
    (Partially pesudo-code)

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

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

    Expert Comment

    by:petmagdy
    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)  );
    }
    0
     
    LVL 5

    Expert Comment

    by:JK2429
    0
     
    LVL 12

    Author Comment

    by:rcmb
    Works great.

    Thanks again,
    Curtis
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Understanding @ResponseBody annotation 5 28
    what is spring 3 29
    MaxSpan challenge 9 36
    Convert BufferedReader to File 1 42
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now