Hacker Rank – Time Conversion – Java/Kotlin

Overview

In this article we will try to solve the Time Conversion problem mentioned in Hacker Rank.

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

Example

Input:   12:00:00AM
Output:  00:00:00

Input:   12:00:00PM
Output:  12:00:00

Input:   12:01:00PM
Output:  12:01:00

Input:   12:01:00AM
Output:  00:01:00

Input:   07:05:45PM
Output:  19:05:45

Code

First get the values of hours, minutes and seconds:

final String[] split = s.substring(0, s.length() - 2).split(":");

Now we need to check whether it’s AM or PM time because the conversion logic depends on that. If it is AM and the hour is 12 then we change the hour bit to 00 and keep the minutes and seconds as it is.

if (split[0].equals("12")) {
    return String.format("00:%s:%s", split[1], split[2]);
}

If it is not 12 then we use the hour value from the input:

return String.format("%s:%s:%s", split[0], split[1], split[2]);

If it is a PM time and the hour is not 12 then we add 12 to the hour value:

String.format("%s:%s:%s", Integer.parseInt(split[0]) + 12, split[1], split[2]);

Full function code as below:

public static String convertTime(String s) {
    final String[] split = s.substring(0, s.length() - 2).split(":");
    if (s.endsWith("AM")) {
        if (split[0].equals("12")) {
            return String.format("00:%s:%s", split[1], split[2]);
        }
        return String.format("%s:%s:%s", split[0], split[1], split[2]);

    } else {
        if (split[0].equals("12")) {
            return String.format("12:%s:%s", split[1], split[2]);
        }
        return String.format("%s:%s:%s", Integer.parseInt(split[0]) + 12, split[1], split[2]);
    }
}

The Kotlin version of the same problem:

private fun convertTime(s: String): String {
    val split = s.substring(0, s.length - 2).split(":")
    return if (s.endsWith("AM")) {
        if (split.first() == "12") {
            String.format("00:%s:%s", split[1], split[2])
        } else String.format("%s:%s:%s", split[0], split[1], split[2])
    } else {
        if (split.first() == "12") {
            String.format("12:%s:%s", split[1], split[2])
        } else String.format("%s:%s:%s", split[0].toInt() + 12, split[1], split[2])
    }
}

Test

Below is a very simple test:

@Test
public void testTimeConversion() {
    assertEquals("12:01:00", convertTime("12:01:00PM"));
    assertEquals("12:00:00", convertTime("12:00:00PM"));
    assertEquals("19:05:45", convertTime("07:05:45PM"));
    assertEquals("00:00:00", convertTime("12:00:00AM"));
    assertEquals("07:00:00", convertTime("07:00:00AM"));
}

Conclusion

In this article we saw how to write a Java and Kotlin function to convert a date in a 12 hour format to a 24 hour format.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: