# Convert time format 12Hr/24Hr

## Overview

In this article we will see how to convert a `12 hour` time format to a `24 hour` one and vice versa. For example a `05:15pm` in a 24 hour format is represented as `17:15`. `12:00 am` is represented as `00:00`. Similarly, `19:15` in 24 hour format is represented as `07:15pm` in a 12 hour format.

## Code

First we need to check in which format we need to convert. We check if the input string has an of `am/AM/pm/PM` then it is a `12 hour` format, if not then it is a `24 hour `format:

```time = time.toLowerCase();
if (time.contains("am") || time.contains("pm")) {
return convert12HourFormatTo24HourFormat(time);
} else {
return convert24HourFormatTo12HourFormat(time);
}
```

To convert to a `12` hour format we will subtract `12` from the hour value if the hour is more than `12` else we will use the same hour and minute value. We also need to append `am` or `pm` based on the value of hour. If the hour is greater and or equal to `12` then it’s a `pm` otherwise `am`:

```private static String convert24HourFormatTo12HourFormat(String time) {
final int hour = Integer.parseInt(time.substring(0, 2));
final String amOrPm = hour >= 12 ? "pm" : "am";
final String minute = time.substring(3, 5);
if (hour == 0 || hour > 12) {
return String.format("%02d:%s%s", Math.abs(hour - 12), minute, amOrPm);
} else {
return String.format("%02d:%s%s", hour, minute, amOrPm);
}
}
```

To convert a `12` hour format to a `24` hour we add `12` to the hour value. We need to cater for a special case where the hour value is `12. In that case,` we will either display `00` for `am` time and `12` for `pm` time:

```private static String convert12HourFormatTo24HourFormat(String time) {
final String minute = time.substring(3, 5);
if (time.startsWith("12")) {
if (time.contains("am")) {
return String.format("%s:%s","00", minute);
} else {
return String.format("%s:%s","12", minute);
}
}
return String.format("%s:%s",Integer.parseInt(time.substring(0, 2)) + 12, minute);
}
```

FunTime.java

```public class FunTime {

public static String convertTime(String time) {
time = time.toLowerCase();
if (time.contains("am") || time.contains("pm")) {
return convert12HourFormatTo24HourFormat(time);
} else {
return convert24HourFormatTo12HourFormat(time);
}
}

private static String convert24HourFormatTo12HourFormat(String time) {
final int hour = Integer.parseInt(time.substring(0, 2));
final String amOrPm = hour >= 12 ? "pm" : "am";
final String minute = time.substring(3, 5);
if (hour == 0 || hour > 12) {
return String.format("%02d:%s%s", Math.abs(hour - 12), minute, amOrPm);
} else {
return String.format("%02d:%s%s", hour, minute, amOrPm);
}
}

private static String convert12HourFormatTo24HourFormat(String time) {
final String minute = time.substring(3, 5);
if (time.startsWith("12")) {
if (time.contains("am")) {
return String.format("%s:%s","00", minute);
} else {
return String.format("%s:%s","12", minute);
}
}
return String.format("%s:%s",Integer.parseInt(time.substring(0, 2)) + 12, minute);
}
}
```

## Test

Below is a simple JUnit test to test the above code:

```import org.junit.Test;

import static org.junit.Assert.*;

public class FunTimeTest {

@Test
public void convertTime() {
assertEquals("17:15", FunTime.convertTime("05:15 pm"));
assertEquals("17:15", FunTime.convertTime("05:15pm"));
assertEquals("00:00", FunTime.convertTime("12:00 am"));
assertEquals("12:00", FunTime.convertTime("12:00 pm"));

assertEquals("07:15pm", FunTime.convertTime("19:15"));
assertEquals("12:00am", FunTime.convertTime("00:00"));
assertEquals("01:00am", FunTime.convertTime("01:00"));
assertEquals("12:00pm", FunTime.convertTime("12:00"));
}
}
```

## Conclusion

In this article we discussed how to convert a 12 hour format to a 24 hour one and vice-versa in Java.