Write a java program to convert string in time and add time

Write a program to read two String variables containing time intervals in hh:mm:ss format. Add the two-time intervals and return a string in days:hours:minutes:seconds format where DD is the number of days.
Hint: Maximum value for hh:mm:ss is 23:59:59.

Input and Output Format:
Input consists of two string.
The output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:
12:45:30
13:50:45
Sample Output 1:
1:2:36:15

Sample Input 2:
23:59:59
23:59:59
Sample Output 2:
1:23:59:58

Convert the string in time and add time in Java.

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;

public class Main {

  public static void main(String[] args) throws IOException, ParseException {
    Scanner sc = new Scanner(System.in);

    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    sdf.setTimeZone(TimeZone.getTimeZone("s1"));
    sdf.setTimeZone(TimeZone.getTimeZone("s2"));
    Date d1 = sdf.parse(s1);
    Date d2 = sdf.parse(s2);
    long add = d1.getTime() + d2.getTime();

    String s = sdf.format(add);

    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse(s));
    int FindDay = cal.get(Calendar.DAY_OF_MONTH);
    if (FindDay > 1)
      FindDay = FindDay - 1;
    String op = FindDay + ":" + s;
    System.out.println(op);
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

seventeen + sixteen =