0

This is a question from hackerank i've some doubt in the code i have written : Sample Input : 01:23:32PM Sample Output : 13:23:32

import java.util.Scanner;
public class Solutions {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String time = sc.nextLine();
        int n = time.length();
        String session = time.substring(n-2);
        if(session == "AM") {
            if(time.substring(0,1) == "12") {
                String x = time.replace("12","00");
                String y = x.substring(0,n-2);        
                System.out.println(y);
            } else {
                System.out.println(time.substring(0,n-2));
            }
        } else if(session == "PM") {
            if(time.substring(0,1) == "12") {
                System.out.println(time.substring(0,n-2));
            }
            else {
                String hour = time.substring(0,2);
                int newformat = Integer.parseInt(hour) + 12;
                String newTime = String.valueOf(newformat);
                System.out.println(time.replace(hour,newTime));
            }
        }
    }
}

0 Answers0