We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Using Java LocalTime class.privatestaticStringtimeConverterLocalTime(Stringinput){DateTimeFormattertimeFormatter=DateTimeFormatter.ofPattern("hh:mm:ssa");DateTimeFormatteroutputFormat=DateTimeFormatter.ofPattern("HH:mm:ss");LocalTimetimeIn24=LocalTime.parse(input,timeFormatter);returntimeIn24.format(outputFormat);}// Using Java algorithmic approach.privatestaticStringtimeConverter(Stringinput){StringtimeIn24="";Stringhours=input.substring(0,2);StringtimeWithoutPrefix=input.substring(0,input.length()-2);if(input.contains("PM")){if(hours.equals("12")){timeIn24=timeWithoutPrefix;}else{inthourIn24=Integer.parseInt(hours)+12;timeIn24=String.valueOf(hourIn24).concat(timeWithoutPrefix.substring(2));}}if(input.contains("AM")){if(hours.equals("12")){timeIn24="00".concat(timeWithoutPrefix.substring(2));}else{timeIn24=timeWithoutPrefix;}}returntimeIn24;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
Java 8 based with 2 different approaches