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.
publicstaticStringtimeConversion(Strings){// Write your code hereStringBuilderfinalTime=newStringBuilder("");booleanisPm=false;for(inti=0;i<s.length();i++){finalTime.append(s.charAt(i));intsecoundLastEle=s.length()-2;if(s.charAt(secoundLastEle)=='A'){isPm=false;}else{isPm=true;}}finalTime.deleteCharAt(finalTime.length()-1);// last deletecharAt finalTime.deleteCharAt(finalTime.length()-1);// secound last deletecharAt StringsubSet=finalTime.substring(0,2);intconvert_time=Integer.parseInt(subSet);if(isPm){if(convert_time!=12){convert_time+=12;}Stringreplace_convert_time=String.valueOf(convert_time);finalTime.replace(0,2,replace_convert_time);}else{if(subSet.equals("12")){finalTime.replace(0,2,"00");}}returnfinalTime.toString();}
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 Solution
Using a boolen value to indicate the time am/pm.