0
我试图将12小时格式中给出的时间转换为24小时格式。用于在24小时内以12小时格式输入时间的输出(不使用任何建立类)
但是我没有得到预期的输出时间是AM。
谁能指导我为什么?
输入形式
HH:MM:SS
MyApproach
要转换在24小时格式我检查如果时间在AM给定的时间, 我打印的按原样输出。 如果时间是下午给我加12个小时的时间
下面是我的函数:
public static String timeConversion(String time)
{
String strnew1="";
String strnew2="";
String strnew="";
String strnew3="";
boolean b1=true;
for(int i=time.length()-2;i<time.length();i++)
{
strnew=strnew+time.charAt(i);
}
if(strnew=="AM")
{
b1=false;
for(int m=0;m<time.length()-2;m++)
{
strnew3=strnew3+time.charAt(m);
}
System.out.println("jhh");
System.out.println(strnew3);
}
else
{
for(int j=0;j<2;j++)
{
strnew1=strnew1+time.charAt(j);
}
int a=Integer.parseInt(strnew1);
int b=a+12;
strnew2=b+strnew2;
for(int k=2;k<time.length()-2;k++)
{
strnew2=strnew2+time.charAt(k);
}
}
if(b1==false)
return strnew3;
else
return strnew2;
}
}
**OutputShown**
Input OutputShown Expected Output
05:32:32AM 17:32:32 05:32:32
为什么不使用预定义的类SimpleDateFormat而不是编写自己的? – Satya
我无法使用that.A测试 –
请正确格式化您的代码,并删除所有不相关的部分,如空循环和无意义的打印语句。 – yole