HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

12/24h Time format converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
formattimeconverter24h

Problem

I am really new at this and I was wondering if there is a more efficient way of writing this code, which converts time format to 12-hour from 24-hour format, and vice-versa.

For example 07:16:35PM converts to 19:16:35, and 13:05:25 to 1:05:25PM.

```
public static void convertTimeTo24(String time)
{

if(time.substring(8,10).equals("PM") && Integer.parseInt(time.substring(0,2)) 23)
{
System.out.print(00+time.substring(2,8));
}
}

else if(time.substring(8,10).equals("AM") && Integer.parseInt(time.substring(0,2)) == 12)
{
System.out.print("00"+time.substring(2, time.length()));
}

else
{
System.out.print(time.substring(0,8));
}
}

public static void convertTimeTo12(String time)
{
int hour24 ;
if(Integer.parseInt(time.substring(0, 2)) > 12 ) //check if the hr is greater than 12
{
hour24 = Integer.parseInt(time.substring(0, 2)) - 12; //substract 12 from the hr
if(hour24 < 10)
{
System.out.println("0"+hour24 + time.substring(2, time.length())+"PM"); //print hour the hour in 24hr format
}

else
{
System.out.println(+hour24 + time.substring(2, time.length())+"PM"); //print hour the hour in 24hr format
}
}

if(Integer.parseInt(time.substring(0, 2)) == 0 )
{
hour24 = Integer.parseInt(time.substring(0, 2)) + 12; //print hour the hour in 24hr format
System.out.println(hour24 + time.substring(2, time.length())+"AM"); //print hour the hour in 24hr format
}

}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String time = in.nextLine(); //sets time to string input
if(time.length()<10) //check if input is in 24hours format or 12
convertTimeTo12(time); //call the convert function above if

Solution

General comments:

  • try to indent your code properly - it is hard to read at the moment



  • follow the language conventions: variable should start in lower case (Hour24 => hour24)



  • similarly, opening braces are at the end of the line, not at the beginning of a new line (like in C-like languages)



  • try to do only one thing in one method - at the moment your methods do the conversion and print at the same time



Regarding your question about efficiency - a more efficient way (less time to code and fewer lines of code) would be to use existing libraries.

With Java 8, you could use the java.time API:

private static final DateTimeFormatter FMT_24 = DateTimeFormatter.ofPattern("HH:mm:ss");
private static final DateTimeFormatter FMT_12 = DateTimeFormatter.ofPattern("h:mm:ssa");

public static String convertTimeTo24(String time12) {
  LocalTime time = LocalTime.parse(time12, FMT_12);
  return FMT_24.format(time);
}

public static String convertTimeTo12(String time24) {
  LocalTime time = LocalTime.parse(time24, FMT_24);
  return FMT_12.format(time);
}


And to avoid duplicating code, you could refactor the two methods into:

public static String convertTimeTo24(String time12) {
  return convertFormats(time12, FMT_12, FMT_24);
}

public static String convertTimeTo12(String time24) {
  return convertFormats(time24, FMT_24, FMT_12);
}

private static String convertFormats(String fromTime, DateTimeFormatter fromFormat,
                                                      DateTimeFormatter toFormat) {
  LocalTime time = LocalTime.parse(fromTime, fromFormat);
  return toFormat.format(time);
}


Your main method needs to be changed accordingly.

Code Snippets

private static final DateTimeFormatter FMT_24 = DateTimeFormatter.ofPattern("HH:mm:ss");
private static final DateTimeFormatter FMT_12 = DateTimeFormatter.ofPattern("h:mm:ssa");

public static String convertTimeTo24(String time12) {
  LocalTime time = LocalTime.parse(time12, FMT_12);
  return FMT_24.format(time);
}

public static String convertTimeTo12(String time24) {
  LocalTime time = LocalTime.parse(time24, FMT_24);
  return FMT_12.format(time);
}
public static String convertTimeTo24(String time12) {
  return convertFormats(time12, FMT_12, FMT_24);
}

public static String convertTimeTo12(String time24) {
  return convertFormats(time24, FMT_24, FMT_12);
}

private static String convertFormats(String fromTime, DateTimeFormatter fromFormat,
                                                      DateTimeFormatter toFormat) {
  LocalTime time = LocalTime.parse(fromTime, fromFormat);
  return toFormat.format(time);
}

Context

StackExchange Code Review Q#127705, answer score: 2

Revisions (0)

No revisions yet.