How to convert a given 12 hour time format in hh:mm:ss AM/PM to 24 hour format if the function only takes a string as an input

lets say time given is 7:49:50 PM the output should be 19:49:50 the problem I am facing is that how to read from string and compute the string values from string to numeric then return a 24 hour format that is also a string.

3/16/2018 6:34:19 PM

Dibya Prakash Paul

7 Answers

New Answer


Use an If statement to see if the string includes pm or am and add 12 to the first number before the “:” if it’s pm


Use the LocalTime class and the DateTimeFormatter class. See... https://code.sololearn.com/c3RAJZrr2o8I/?ref=app


in Java there is a class to deal with time and date...... check this link out..... https://beginnersbook.com/2014/


Thanks everyone, finally got a working code! https://code.sololearn.com/cO9CVRNBHiX9/?ref=app


Doesn't work for me! Reports compilation error!


@Ariela .. yup that's a good one 👍


Try running on a pc, i am getting the results though!