+ 3
Can anyone tell me the errors please?
10 Respuestas
+ 2
Unable to get you Eashan Morajkar
+ 2
For input 1:00 AM
It shud output 01:00
But in your code it will be 1:00
Just append 0 before for these kind of cases:)
+ 2
Nivya Can you post your Sol so that I can find alternatives for my Sol?
+ 1
Nivya for 2:00 AM. It should print 02:00 ?
0
Yes...
check that output format in description
XX:XX
it's like this right so our output should be appended with 0 if it's 1 number
do like this if it's AM and length is less then 2 just append tht number with 0
if(AM){
if((length before ":") !=2){
append 0
}}
something like this
0
No other option! we left with, its a format to show time in xx:xx this format ^^
0
Atul
It seems working now.
--------
There is another solution with SimpleDateFormat
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
public class Program
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
DateFormat df = new SimpleDateFormat("hh:mm aa");
DateFormat outputformat = new SimpleDateFormat("HH:mm");
Date date = null;
String output = null;
try {
date = df.parse(input);
output = outputformat.format(date);
System.out.println(output);
} catch(ParseException pe) {
pe.printStackTrace();
}
}
}
- 2
Well it did not use to work but now somehow it worked
- 2
I get the answer correct
- 2
Sure Atul here u go=>
//Code
import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String str=s.nextLine();
String []in=str.split(":");
String []in1=in[1].split(" ");
String day="AM";
String nyt="PM";
String d= str.substring(str.length()-2,str.length());
if(d.equals(day)){
if(in[0].length()!=2)
{ System.out.print("0"+str.substring(0,str.length()-3));
}
else
System.out.print(str.substring(0,str.length()-3));
}
if(d.equals(nyt)){
int i=Integer.parseInt(in[0])+12;
String res=Integer.toString(i)+":"+in1[0];
System.out.print(res);
}
}
}