-2

I needed to convert only the hour part of 12 hour time string to 24 hour format. I'm not interested in other than hour parts of a date string. SimpleDateFormatter is notoriously buggy and I in fact I'm more interested in the conversion algorithm itself. So what I want is to find the algorithm to convert the hour part. This is example of an uncleaned string containing hour part: "12:15PM". Sometimes there is spaces between minutes and meridiem sometimes not. Sometimes it has also character 160. So before conversion method I check and clean the string parts and then feed then meridiem(am or pm) and hour to the method which returns hour as 24 hour format int.

Tonecops
  • 127
  • 9
  • Does this answer your question? [convert 12-hour hh:mm AM/PM to 24-hour hh:mm](https://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm). I know that question is about JavaScript, but the algorithm is the same. – Ole V.V. Jul 14 '20 at 05:51

2 Answers2

0

This is my naive style code for beginners to convert an hour in 12 hour format to an hour in 24 format.

public static int convert12to24(String meridiem, String hour) {
    //meridiem is that am or pm,
    meridiem = meridiem.toLowerCase();
    int h_12 = 0;
    int h_24 = 0;
    try {
        h_12 = Integer.parseInt(hour);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    if (h_12 == 12) {
        //this is the midnight hour
        if (meridiem.contains("am")) {//generally before noon
            h_24 = 0;
        } else {//this is the hour starting at noon
            h_24 = 12;
        }
    } else if (h_12 >= 1)//all the rest
    {
        if (meridiem.contains("am")) {
            //hour starting after first hour at midnight to 11 facing noon
            h_24 = h_12;
        } else {//pm hours starting right after first hour after noon
            h_24 = h_12 + 12;
        }
    }

    return h_24;
}
Tonecops
  • 127
  • 9
  • Thanks for answering your own question. The code is correct for correct input. I’d like to see better validation. `convert12to24("xx", "01")` ought to throw an exception, but it just returns 13. `convert12to24("lame arg", "01")` returns 1. `convert12to24("PM", "29")` is meaningless but returns 41. – Ole V.V. Jul 13 '20 at 02:33
  • 1
    Of course it is not anything else than condensed pseudocode like starting point. Any one can add their checks before or after or inside the method. Thanks for your code example. Thing is that in great hurry people tend to stick to their old working things. The goal is to extract the core of the algorithm available for those who don't have energy to study java.time. – Tonecops Jul 13 '20 at 05:52
0

java.time

I recommend using java.time, the modern Java date and time API, rather than your own calendar implementation. It can be used in all locales.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mma", Locale.ENGLISH);
    
    String timeString12Hours = "12:15PM";
    
    LocalTime time = LocalTime.parse(timeString12Hours, formatter);
    System.out.println("Time: " + time);
    int hour = time.getHour();
    System.out.println("Hour of day in 24 hour format: " + hour);

Output is:

Time: 12:15
Hour of day in 24 hour format: 12

What we get for free from java.time is validation. This code will detect if meridiem is not either AM or PM, or the hour is outside the range 1 through 12 or not a number at all.

Under no circumstances use the SimpleDateFormat class mentioned in the question. It’s a notorious troublemaker of a class and long outdated.

Edit: If for compatibility with old code that I don’t know and that you haven’t got time to clean up at the moment you need the method signature from your own answer:

private static DateTimeFormatter meridiemHourFormatter
        = DateTimeFormatter.ofPattern("ahh", Locale.ENGLISH);

public static int convert12to24(String meridiem, String hour) {
    String meridiemHour = meridiem + hour;
    return LocalTime.parse(meridiemHour, meridiemHourFormatter).getHour();
}

Trying it out:

    System.out.println(convert12to24("PM", "12"));

12

Every reader can decide for himself or herself: Which method implementation takes less energy to understand if in a hurry, yours or mine?

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • You seem to be confused about the goal (what is algorithm) of the question so I modified the question to describe goal more accurately. Question and accepted answers must be solely about algorithm. Using black box library is not about algorithm. – Tonecops Jul 14 '20 at 05:46
  • By the way about the understanding part by definition there is no difference if the signature of the method ie. API is the same. – Tonecops Jul 14 '20 at 05:58