Last active
June 3, 2016 15:32
-
-
Save slavaceornea/5ec2469e5aab25430ef5ee3c3733749e to your computer and use it in GitHub Desktop.
Given a time in AM/PM format, convert it to military (24-hour) time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* | |
* @author Slava | |
* | |
* Given a time in AM/PM format, convert it to military (24-hour) time. | |
* | |
* Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. | |
* | |
* Input Format | |
* A single string containing a time in -hour clock format. | |
* | |
* Output Format | |
* | |
* Convert and print the given time in -hour format, where . | |
* | |
* Sample Input | |
* 07:05:45PM | |
* | |
* Sample Output | |
* 19:05:45 | |
*/ | |
public class TimeConversion { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
String time = in.next(); | |
System.out.println(convertTo24HourFormat(time)); | |
} | |
private static String convertTo24HourFormat(String timeIn12HourFormat) | |
{ | |
int hour = Integer.parseInt(timeIn12HourFormat.substring(0, 2)); | |
String timeOfDay = timeIn12HourFormat.substring(8, 10); | |
if(timeOfDay.equals("AM")) | |
{ | |
if(hour == 12) | |
hour = 0; | |
} | |
else if(timeOfDay.equals("PM")) | |
{ | |
if(hour != 12) | |
hour += 12; | |
} | |
return String.format("%02d%s", hour, timeIn12HourFormat.substring(2, 8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment