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 :

package com.javamultiplex.datetime;

import java.util.Scanner;

 * @author Rohit Agarwal
 * @category Date and Time
 * @problem How to subtract two given times?
public class SubstractTime {

 //1 Hour = 60 minutes = 60 * 60 =3600 seconds 
 public static final int secondsInHour=3600;
 //1 Minute = 60 seconds
 public static final int secondsInMinute=60;
 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(;
   System.out.println("Enter first time(24 hour) in HH:mm:ss format");
   String time1 =;
   System.out.println("Enter second time(24 hour) in HH:mm:ss format");
   String time2 =;
   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 secondsInTime1=hours1*secondsInHour + minutes1*secondsInMinute +seconds1;
    int secondsInTime2=hours2*secondsInHour + minutes2*secondsInMinute +seconds2;
    int seconds=secondsInTime1-secondsInTime2;
    int hours=seconds/secondsInHour;
    int remainingSeconds=seconds-(hours*secondsInHour);
    int minutes=remainingSeconds/secondsInMinute;
    // Converting each integer value of String and combining all strings.
    String finalTime = String.valueOf(hours) + ":" + String.valueOf(minutes) + ":" + String.valueOf(remainingSeconds);
    System.out.println("New time is :\n" + finalTime);
    System.out.println(hours + " Hours " + minutes + " Minutes " + remainingSeconds + " Seconds ");

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


 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 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.