How to subtract two given times in HH:mm:ss format in Java?

Problem Description :

Write a program in Java that subtracts two times that are given in HH:mm:ss format with proper validations. For example subtraction of 12:34:56 and 23:45:23 is -11:-10:-27 or -11 Hours -10 Minutes -27 Seconds.

Concept :

The algorithm that we are using for solving this problem is very simple and straight forward.
  • Split both given strings by delimiter ":".
  • Now we have to convert hours, minutes and seconds to Integer.
  • Now convert both the times in seconds, then subtract both seconds and get a new value of seconds.
  • Now divide seconds into hours, minutes and seconds.
  • Convert hours, minutes and seconds into String and combine using ":".

Java Program :

Output :

Output of Java program that subtracts two given times in HH:mm:ss format.
Output - How to subtract two given times in Java?

References :

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Date and Time problems, Time subtraction in Java, Solution in Java, String, if else statement, Regular expressions.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.

Let's Get Connected: Linkedin | Facebook |

How to subtract two given times in HH:mm:ss format in Java? How to subtract two given times in HH:mm:ss format in Java? Reviewed by Rohit Agarwal on 1/17/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.