Java code to find difference between two time,where time is stored in date type and time is in 24 hour format

427 views Asked by At

i need java code to find difference between two times in 24 hour format for example: 20:00:00 - 04:00:00

and the expected output is 8 hrs but now output is 16 hrs when i tried 12 hour format out put is coming 4.

below is the code used to parse and to find difference

SimpleDateFormat readFormat = new SimpleDateFormat("HH:mm");
Date d1 = readFormat.parse(txtshiftIn);
Date d2 = readFormat.parse(txtshiftOut);
long diff = d2.getTime() - d1.getTime();

input is just 20:00 and 04:00 no seconds and AM/PM part.

2

There are 2 answers

10
locus2k On BEST ANSWER

The issue with using Date is it still expects an actual date even though you are using just the time portion of it, so if you're just sending it the time it will not be correct.

Instead use LocalTime and ChronoUnit.HOURS to get the difference between the time.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

long diff = ChronoUnit.HOURS.between(lt1, lt2);

This will show -16 which means that lt1 is ahead of lt2 which indicates lt2 is the next day so we can modify it to get the differences as such

if (diff < 0) {
  diff += 24;
}

This will give you the 8 hour difference you're expecting.

UPDATE To account the difference in minutes you can do the following:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

long diff = ChronoUnit.MINUTES.between(lt1, lt2); //get diff in minutes

if (lt2.isBefore(lt1)) {
  diff += TimeUnit.DAYS.toMinutes(1); //add a day to account for day diff
}

long hours = diff / 60;
long minutes = diff % 60;

LocalTime newTime = LocalTime.parse(String.format("%02d:%02d", hours, minutes), dtf); //Format the difference to be converted to LocalTime

System.out.println(newTime);

This will produce the difference in minutes:

08:30

Update 2

Here is a cleaner method that returns a Duration

public Duration timeDifference(String txtshiftIn, String txtshiftOut) {
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
  LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
  LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

  Duration between = Duration.between(lt1, lt2);

  if (lt2.isBefore(lt1)) { //account for lt2 being on the next day
    between = Duration.ofMinutes(TimeUnit.DAYS.toMinutes(1)).plus(between);
  }

  return between;
}
0
AudioBubble On

While it is strongly recommended you use a more up-to-date Java time library like LocalTime your logic so far actually is correct with one little caveat:

SimpleDateFormat readFormat = new SimpleDateFormat("HH:mm");
Date d1 = readFormat.parse(txtshiftIn);
Date d2 = readFormat.parse(txtshiftOut);
long diff = d2.getTime() - d1.getTime();

In your code if d1 is after d2 you will get a negativ result for long diff

So when you say

"but now output is 16 hrs"

The actual output is -16 hrs

Of course minus sixteen hours doesn't make much sense in your case, but you can easily fix that with the simple trick of just adding 24 hours in case of a negative result for diff. (And -16+24 is the 8 you expected as a result).

So just add the following lines at the end of your posted code

if(diff < 0) {
    diff = 86400000 + diff;
} 

And you will get the results you expect! (86400000 is the 24h expressed in milliseconds)