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 :


package com.javamultiplex.datetime;

import java.util.Scanner;

/**
 * 
 * @author Rohit Agarwal
 * @category Date and Time
 * @problem How to add two given times in java?
 *
 */
public class AddTimes {

 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(System.in);
   System.out.println("Enter first time(24 hour) in HH:mm:ss format");
   String time1 = input.next();
   System.out.println("Enter second time(24 hour) in HH:mm:ss format");
   String time2 = input.next();
   if (isTimeValid(time1) && isTimeValid(time2)) {

    // Separating first String using delimiter ":"
    String[] firstTimeParts = time1.split(":");
    // Converting String to Integer
    int hours1 = Integer.parseInt(firstTimeParts[0]);
    int minutes1 = Integer.parseInt(firstTimeParts[1]);
    int seconds1 = Integer.parseInt(firstTimeParts[2]);

    // Separating second String using delimiter ":"
    String[] secondTimeParts = time2.split(":");
    // Converting String to Integer
    int hours2 = Integer.parseInt(secondTimeParts[0]);
    int minutes2 = Integer.parseInt(secondTimeParts[1]);
    int seconds2 = Integer.parseInt(secondTimeParts[2]);

    int hours = hours1 + hours2;
    int minutes = minutes1 + minutes2;
    int seconds = seconds1 + seconds2;
    int days = 0;

    /**
     * 
     * 60 seconds=1 minute. So if value of seconds>59 adding 1
     * minute to minutes. 60 minutes=1 hour So if value of
     * minutes>59 adding 1 hour to hours. 24 hours=1 day So if value
     * of hours>23 adding 1 day to days.
     * 
     */
    if (seconds > 59) {
     seconds = seconds - 60;
     minutes = minutes + 1;
     if (minutes > 59) {
      minutes = minutes - 60;
      hours = hours + 1;
      if (hours > 23) {
       hours = hours - 24;
       days = days + 1;
      }
     } else {

      if (hours > 23) {
       hours = hours - 24;
       days = days + 1;
      }

     }
    } else {
     if (minutes > 59) {
      minutes = minutes - 60;
      hours = hours + 1;
      if (hours > 23) {
       hours = hours - 24;
       days = days + 1;
      }
     } else {

      if (hours > 23) {
       hours = hours - 24;
       days = days + 1;
      }

     }
    }

    // Converting each integer value of String and combining all Strings.
    String finalTime = String.valueOf(days) + ":" + String.valueOf(hours) + ":" + String.valueOf(minutes)
      + ":" + String.valueOf(seconds);
    System.out.println("New time is :\n" + finalTime);
    System.out.println("OR");
    System.out.println(days + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds ");

   } else {
    System.out.println("Time is not valid.");
   }
  } finally {
   if (input != null) {
    input.close();
   }
  }

 }

 private static boolean isTimeValid(String time) {

  boolean result = false;
  /*
   * Regular expression that matches String with format HH:mm:ss 
   * HH -> 0-23  
   * mm -> 0-59 
   * ss -> 0-59
   */
  String pattern = "(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9]):(0?[0-9]|[1-5][0-9])";
  if (time.matches(pattern)) {
   result = true;
  }
  return result;
 }

}


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

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.