problem-
Given a time in -hour AM/PM format, convert it to military (-hour) time.
Note: Midnight is on a -hour clock, and on a -hour clock. Noon is on a -hour clock, and on a -hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .
Sample Input
07:05:45PM
Sample Output
19:05:45
solution-
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/*
*
* Ratnadeep Sen
* National Institute Of Technology Silchar -India (NITS)
*
*/
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
String inputTime = input.nextLine();
String [] tempArray = inputTime.split(":");
String hours = tempArray[0];
String minutes = tempArray[1];
String seconds = tempArray[2].substring(0, 2);
int tempHours;
if (tempArray[2].substring(2, 4).equalsIgnoreCase("PM")) {
if (Integer.parseInt(hours) < 12) {
tempHours = Integer.parseInt(hours);
tempHours += 12;
hours = Integer.toString(tempHours);
}
}
if (tempArray[2].substring(2, 4).equalsIgnoreCase("AM")) {
if (Integer.parseInt(hours) == 12) {
hours = "00";
}
}
System.out.println(hours + ":" + minutes + ":" + seconds);
}
}
No comments:
Post a Comment