Java Program To Add Times

Write Java program to add times

// WAP to add two different times in HH:MM:SS format

import java.util.Scanner;
public class Time
{
    public static void main(String[] args) 
    {
        int hour, minute, second;
        int hour1, minute1, second1;
        int hour2, minute2, second2;

        Scanner sc = new Scanner(System.in);


        System.out.println("Enter the time-1 in HH:MM:SS format :--> ");
        hour1 = sc.nextInt(); 
        minute1 = sc.nextInt();
        second1 = sc.nextInt();

        System.out.println("Enter the time-2 in HH:MM:SS format :--> ");
        hour2 = sc.nextInt();
        minute2 = sc.nextInt();
        second2 = sc.nextInt();

        second = second1 + second2;
        minute = minute1 + minute2 + (second/60);
        hour = hour1 + hour2 + (minute/60);
        minute = minute % 60;
        second = second % 60;

        System.out.println("HOUR:MINUTE:SECOND :--> "+hour+" : "+minute+" : "+second);

        sc.close(); // prevent memory leakage
    }
    
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.