I'm developing an app in which I'm using Java8 Time. I'm facing an issue.
Let's say Time A is 08:00 and Time B is 17:00, so the difference of between these two times will be 9h which in my case is correct, but if Time A is 18:00 and Time B is 02:00 it should be 8h, but in my case my program is returning -16. Kindly someone guide me how to solve this.
My code:
@Test
public void testTime()
{
DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm");
String s = "18:00";
String e = "02:00";
// Parse datetime string to java.time.LocalDateTime instance
LocalTime startTime = LocalTime.parse(s, format);
LocalTime endTime = LocalTime.parse(e, format);
String calculatedTime = ChronoUnit.HOURS.between(startTime, endTime)%24 + ":"
+ ChronoUnit.MINUTES.between(startTime, endTime)%60;
System.out.println(calculatedTime);
}