Skip to content
Advertisement

What is the algorithm represented in Java to convert an 12 hour am/pm meridiem format hour to 24 hour format?

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.

Advertisement

Answer

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

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement