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



Problem Description :


Write a program in Java that adds two times that are given in format HH:mm:ss with proper validations. For example - addition of 11:12:45 and 23:45:44 is 1:10:58:29 or 1 Day 10 Hours 58 Minutes 29 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.
  • Do an addition of hours, minutes and seconds. If the value of hours>23, add 1 to days because 1 day=24 hours, if the value of minutes > 59, add 1 hour to hours because 1 hour=60 minutes and if the value of seconds > 59, add 1 minute to minutes because 1 minute=60 seconds.
  • Convert hours, minutes and seconds into String and combine using ":".


Java Program :




Output :


Output of Java program that adds two given times in HH:mm:ss format.
Output - How to add 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 addition 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 add two given times in HH:mm:ss format in Java? How to add two given times in HH:mm:ss format in Java? Reviewed by Rohit Agarwal on 1/15/2017 Rating: 5

2 comments:

  1. I am glad that I found your blog, it offers salvation for the problems that are still very hard for me to handle on my own.

    ReplyDelete

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.