14 Maret 2019

HackerRank Substring diff || Time Conversion

Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Function Description
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

Answer

String hasil="";
String ampm = (s.substring(s.length()-2));
String hh1 = s.substring(0,2);
String mn1 = s.substring(3,5);
String sc1 = s.substring(6,8);
int hh2 = Integer.parseInt(hh1);
int hh3 = 0;
if(ampm.equals("PM")){
if(hh1.equals("12")){
hh3=12;
}else{
hh3 = hh2+12;
}
}else if(ampm.equals("AM")){
if (hh1.equals("00")){
hh3 =12;
}else if(hh1.equals("12")){
hh3=0;
}else{
hh3 = hh2;
}
}
if(hh3<10){
hasil="0"+hh3+":"+mn1+":"+sc1;
}else{
hasil=hh3+":"+mn1+":"+sc1;
}
return hasil;


Thanks :)



Tidak ada komentar:

Posting Komentar

Answer HackerRank Cat and Mouse

Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat w...