Java - 12 H to 24 Hour Format (hackerrank)

Rah*_*l P 0 java time time-format

Note : I looked as much as I could for 2 days to check if this is a duplicate. If I missed something, I apologize. This question is to find what the issue is with my attempt at a solution, not one of the existing solutions out there.

My Question

I was trying to solve some problems on hackerrank in Java 7 and I came across the time conversion problem where the problem statement is:

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

Sample Input 07:05:45PM

Sample Output 19:05:45

I looked at solutions involving libraries (such as java.text.SimpleDateFormat, Calendar etc.) but I am trying to do it on my own without them. The issue that I am facing here is that my solution is failing on some test cases but is working on others. In short, it is not the correct solution. I see other solutions but I want to know why mine fails as the correct answer. Could you please help me by telling me where this would fail and how I can go about correcting it?

Some of the solutions that I looked at are:

Conversion from 12 hours time to 24 hours time in java

Convert 12 hours to 24 hours

My code is here:

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

    static String timeConversion(String s) {

        //get the string into an array using : as a separator
        String[] time_array = s.split(":");

        //military_time variable to be returned
        String military_time = new String();

        //final HH part
        String hh_final = new String();
        //Rest after HH to be concatenated to get military_time
        String rest = new String();


        StringBuilder REST_mil_builder = new StringBuilder();
        for (int i = 2; i < 8; i++) {
            REST_mil_builder.append(s.charAt(i));
        }
        //"rest" basically gets everything after HH excluding AM/PM, so 01:03:40PM would have a "rest" value of ":03:40"
        rest = REST_mil_builder.toString();

        int hh = Integer.parseInt(time_array[0]);
        String AMPM_contains = time_array[2];

        //converting if the last piece after the split contains "PM"
        if (AMPM_contains.contains("PM")) {
            hh = hh + 12;
            hh = hh == 24 ? 0 : hh;
        }

        //converting hh to have a 0 before it because when it is an integer 01 will be just 1 which we don't want
        StringBuilder hh_build = new StringBuilder();
        if (hh >= 0 && hh <= 9) {
            hh_build.append("0");
            hh_build.append(hh);
            hh_final = hh_build.toString();
        } else {
            hh_build.append(hh);
            hh_final = hh_build.toString();
        }

        //military time concatenation
        military_time = hh_final + rest;
        //Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock
        military_time = s == "12:00:00AM" ? "00:00:00" : military_time;
        //Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
        military_time = s == "12:00:00PM" ? "12:00:00" : military_time;

        return military_time;

    }

    private static final Scanner scan = new Scanner(System.in);

    public static void main(String[] args)  {

        //tried several 12 hour time formats here
        String result = timeConversion("01:30:59PM");
        System.out.println(result);
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*eas 6

您的代码失败,因为它无法正确处理 12 小时,即12:xx:xxAM应该映射到00:xx:xx,并且12:xx:xxPM应该映射到,正如Ole VV12:xx:xx在回答中指出的那样

这里不是尝试修复过于复杂的代码,而是一种不同的方法,不使用SimpleDateFormat.

将前 2 位数字解析为数字。如果数字是 12,则将其设置为 0。实现此目的的一个技巧是使用模 12。如果输入以 结尾PM,则添加 12。现在重建字符串,用新数字替换前 2 位数字,并删除 AM/PM 后缀。

像这样:

public static String timeConversion(String s) {
    int hour = Integer.parseInt(s.substring(0, 2)) % 12;
    if (s.endsWith("PM"))
        hour += 12;
    return String.format("%02d", hour) + s.substring(2, 8);
}
Run Code Online (Sandbox Code Playgroud)

使用 Ole VV 的测试

System.out.println(timeConversion("12:30:59AM"));
System.out.println(timeConversion("11:30:59AM"));
System.out.println(timeConversion("12:30:59PM"));
System.out.println(timeConversion("11:30:59PM"));
Run Code Online (Sandbox Code Playgroud)

输出

public static String timeConversion(String s) {
    int hour = Integer.parseInt(s.substring(0, 2)) % 12;
    if (s.endsWith("PM"))
        hour += 12;
    return String.format("%02d", hour) + s.substring(2, 8);
}
Run Code Online (Sandbox Code Playgroud)