Results 1 to 2 of 2
  1. #1
    emyk is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default negetive hours when subtracting dates

    I am trying to find the difference between two given days in hours. For the most part it returns the correct values but when values are combination of AM and PM it returns negetive value (I am using military hours).

    Do i need to change the 24 hours to standard 12 hours to fix this issue or is there any other way to get arround it?

    DateFormat df1 = new SimpleDateFormat("yyyyMMdd hh:mm");
    java.util.Date FTIME= df1.parse(ConvDateTime);
    java.util.Date nFTIME= df1.parse(nConvDateTime);

    Here are some date values that returns negetive
    FTIME = 20110403 08:00
    nFTIME = 20110403 12:02
    returns = -7

    FTIME = 20110404 11:30
    nFTIME = 20110404 03:30
    returns = -8

    long remainder = (( nFTIME.getTime() - FTIME.getTime() )/ (60 * 60 * 1000));


  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    In the second example you are subtracting just before lunch from very early morning, so it no wonder you get a negative result.

    For the other one you might like to read the SimpleDateFormat to see how "military" hours are actually specified in the constructor. I think you are subtracting breakfast time from just past midnight: which, again, is negative.

Similar Threads

  1. getting a date value from db and subtracting it in jsp
    By dayalan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-14-2011, 10:05 AM
  2. Subtracting 2d Arrays
    By Spyderpig in forum New To Java
    Replies: 9
    Last Post: 03-11-2011, 06:12 AM
  3. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 08:35 AM
  4. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 07:26 PM
  5. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 11:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts