Time Converion Problem Hackerrank Solution

 Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

Example

  • Return '12:01:00'.

  • Return '00:01:00'.

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):

  • string s: a time in  hour format

Returns

  • string: the time in  hour format

Input Format

A single string  that represents a time in -hour clock format (i.e.:  or ).

Constraints

  • All input times are valid

Sample Input

07:05:45PM

Sample Output

19:05:45
Solution :

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'timeConversion' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */

    public static String timeConversion(String s) {
    // Write your code here
  String hourStr = s.substring(0, 2);   // Extract HH
        String minutesAndSeconds = s.substring(2, 8); // Extract MM:SS
        String meridian = s.substring(s.length() - 2); // Extract AM/PM

        // Convert hour string to integer
        int hour = Integer.parseInt(hourStr);

        // Convert AM/PM to 24-hour format
        if (meridian.equals("AM")) {
            if (hour == 12) {
                hourStr = "00"; // Convert 12 AM to 00
            }
        } else { // PM case
            if (hour != 12) {
                hour += 12; // Convert PM hour (except 12 PM) by adding 12
            }
            hourStr = Integer.toString(hour); // Convert back to string
        }

        // Return final formatted time
        return String.format("%02d", Integer.parseInt(hourStr)) + minutesAndSeconds;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();

        String result = Result.timeConversion(s);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}


method 2 -

Using if else without string format for hr 2 digit

package com.java; public class ClockTime { public static String timeConversion(String s) { // Write your code here String hr = s.substring(0, 2); // Extract HH String minutesAndSeconds = s.substring(2, 8); // Extract MM:SS String meridian = s.substring(s.length() - 2); // Extract AM/PM // Convert hour string to integer int hour = Integer.parseInt(hr); // Convert AM/PM to 24-hour format if (meridian.equals("AM")) { if (hour == 12) { hour = 00; // Convert 12 AM to 00 } } else { // PM case if (hour != 12) { hour += 12; // Convert PM hour (except 12 PM) by adding 12 } hr = Integer.toString(hour); // Convert back to string } // Return final formatted time //return String.format("%02d", Integer.parseInt(hourStr)) + minutesAndSeconds; hr=Integer.toString(hour); if (hr.length()<2) { hr="0"+hr; } else{ hr=hr; } return hr + minutesAndSeconds; } public static void main(String[] args) { // TODO Auto-generated method stub ClockTime clockTime = new ClockTime(); System.out.println(clockTime.timeConversion("07:12:34PM")); System.out.println(clockTime.timeConversion("07:12:34AM")); System.out.println(clockTime.timeConversion("12:12:34AM")); } }
   

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence