Time Conversion Solution in Java | HackerRank | Time Conversion HackerRank Solution


Time Conversion Solution in Java | HackerRank | Time Conversion HackerRank Solution

HackerRank Algorithm's Solutions

Time Conversion Solution in Java
Time Conversion HackerRank Solution in Java


Problem:

Given a time in -hour 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. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.

Function Description

Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

  • s: a string representing time in  hour format

Input Format

A single string  containing a time in -hour clock format (i.e.:  or ), where  and .

Constraints

  • All input times are valid

Output Format

Convert and print the given time in -hour format, where .

Sample Input 0

07:05:45PM

Sample Output 0

19:05:45


https://www.hackerrank.com/challenges/time-conversion/problem

Time Conversion HackerRank Solution in Java 
Time Conversion  Solution in Java

import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); String time = input.nextLine(); int hour = Integer.parseInt(time.substring(0,2)); int minute = Integer.parseInt(time.substring(3,5)); int second = Integer.parseInt(time.substring(6,8)); String s = time.substring(8,10); hour += ((s.equals("PM") && hour != 12)?12:0); hour -= ((s.equals("AM") && hour == 12)?12:0); System.out.println(String.format("%02d",hour) + ":" + String.format("%02d",minute) + ":" + String.format("%02d",second)); } }

Post a Comment

0 Comments