Skip to content
Advertisement

Time difference in AM/PM in Java

I am trying to calculate the time difference in java by input time format in 12 hours it works well when i input start time 11:58:10 pm and end time 12:02:15 am. But as i enter 12:00:00 am and 12:00:00 pm it give difference 0 minutes. don’t know why. Below is my code please let me know where i am wrong.

JavaScript

Advertisement

Answer

The new java.time methods LocalTime, DateTimeFormatter and Duration provide much better methods for handling this. For example:

JavaScript

Note: Duration.toSecondsPart requires Java 9 or later, the rest of this code requires Java 8 or later.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement