4
\$\begingroup\$

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)) < 12)
        {
            String hour12 = time.substring(0,2);
            int hour24 = (Integer.parseInt(hour12)) + 12;

            if(hour24<24)
                {
                System.out.print(hour24+time.substring(2,8));
                }

            if(Hour24>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 its 12 hrs format
           else
           convertTimeTo24(time); // calls function to convert 12hr to 24hr format

}

}

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Shouldn't it be K instead of h in FMT_12? \$\endgroup\$ – Tunaki May 6 '16 at 17:41
  • \$\begingroup\$ Mmm I don't know to be honest - I never use AM/PM so not sure if noon should be 12 or 0 - but probably 12 so it would be h. \$\endgroup\$ – assylias May 6 '16 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.