-2

I wanted to subtract and get the hours for this two date:

  • Time In: Jan 1 2014 5:45 PM
  • Time Out:Jan 2 2014 2:00 AM

I already tried using this formula and I always get a negative number. (Example: -5:0-30)

long diffHours = diff / (60 * 60 * 1000) % 24;                       
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Borsalino
  • 1
  • 2
  • 2
  • 3
  • Please show a short but complete program demonstrating the problem. You haven't shown *nearly* enough for us to help you at the moment. – Jon Skeet Jul 14 '14 at 06:01
  • why haven't you tried [this](http://stackoverflow.com/a/5351516/2764279) – earthmover Jul 14 '14 at 06:03
  • possible duplicate of [Calculate date/time difference in java](http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java) – Barranka Jul 14 '14 at 06:04

6 Answers6

1

You may try like this:

Date d1= // start date 
Date d2= // end date 
long dur = d1.getTime() - d2.getTime();

long diffInSec = TimeUnit.MILLISECONDS.toSeconds(dur);
long diffInMin = TimeUnit.MILLISECONDS.toMinutes(dur);
long diffInHour = TimeUnit.MILLISECONDS.toHours(dur);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

see reference

Use Math.abs(argument here) to find their absolute values

Math.abs(175)=175
Math.abs(-184)=184
Math.abs(-0)=0
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54
0

Don't know if this will be much help but from my excel days I used to use this trick a lot. You could set up an if statement to check if the day changes if so then do the sum of (12- booking on time)+(12-booking off time.)

I'm sure there are easier ways to do this but I don't have access to my computer at the minute so would not be able to experiment with your formula.

You could always look into date time format as well and format Into just the time. hh:mm then perform an if again to check for changes in the day.

Chris_livermore
  • 171
  • 1
  • 13
0

You can try something like this

    DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
    Date day1=df.parse("2014-01-01 5:45:00 PM"); // start date
    Date day2=df.parse("2014-01-02 2:00:00 AM"); // end date

    long milliseconds=day2.getTime()-day1.getTime(); // time gap in mil-seconds 

    int seconds = (int) (milliseconds / 1000) % 60 ;
    int minutes = (int) ((milliseconds / (1000*60)) % 60);
    int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

U can also calculate the time difference using SimpleDateFormat: here is the sample code :

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm a");
        System.out.println(""+sdf.format(new Date()));
        String dateStart ="Jan 1 2014 5:45 PM", dateStop="Jan 2 2014 2:00 AM";

        try {
            Date d1 = sdf.parse(dateStart);
            Date d2 = sdf.parse(dateStop);

            //in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
                        diffHours = diffHours+24*diffDays;
//          System.out.print(diffDays + " days, ");
            System.out.print(diffHours + " hours, ");
            System.out.print(diffMinutes + " minutes, ");
            System.out.print(diffSeconds + " seconds.");

        } catch (Exception e) {
            e.printStackTrace();
        }
Krishna Kant
  • 105
  • 9
0

You can use Apache Commons javadoc here:

String formatPeriod = DurationFormatUtils.formatPeriod(dateAsMilis1,
            dateAsMilis2), "HH:mm:ss:SSS");
hexin
  • 947
  • 7
  • 17