如何在j2me中将12小时转换为24小时? [英] How to convert 12 hours time to 24 hours time in j2me?
问题描述
我正在从服务器上获取时间,就像这样
12:00PM-3:00PM,7:00PM-10:30PM
这次为12小时格式.我对该字符串进行了一些操作,并且每次都分开.现在,我有以下四个字符串
(aftr1=> 12:00PM aftr2=> 3:00PM) (evng1=> 7:00PM evng2=> 10:30PM)
现在,我想在24小时内将其转换,因为我想检查用户输入的时间是下午12:00 pm到3:00 pm或晚上7:00 pm到10.30pm. >
或者其他任何想法如何在j2me中完成此任务?
容易,从下午1点到晚上11:59,您需要增加12个小时.然后从12:00 am到12:59m减去12小时.
要在J2ME中完成此操作,请使用String解析工具,因为API中不存在普通的Java实用程序SimpleDateFormat.
http://docs. oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/index.html
String date = "12:00PM";
if (date.indexOf("PM")!=-1)//its pm
{
int pmindex = date.indexOf("PM");
int separator=date.indexOf(":");
int hour = Integer.parseInt(date.substring(0, separator));
int minute = Integer.parseInt(date.substring(separator+1,pmindex));
hour = hour+12;
System.out.println("Time is: " + hour);
}
I am getting time from server it's like this
12:00PM-3:00PM,7:00PM-10:30PM
This time in 12 Hours format. I applied some operation on this string and I separated all times. Now I have four strings like following
(aftr1=> 12:00PM aftr2=> 3:00PM) (evng1=> 7:00PM evng2=> 10:30PM)
Now I want to convert them in 24 hours time, because I want to check the time which was entered by user it is between afternoon time 12:00pm to 3:00pm or in evening 7:00pm to 10.30pm.
Or any other idea how to complete this task in j2me?
Easy, from 1pm until 11:59pm you add 12 hours. Then from 12:00am until 12:59m you subtract 12 hours.
To accomplish this in J2ME use the String parsing tools as the normal Java utility SimpleDateFormat does not exist in the API.
http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/index.html
String date = "12:00PM";
if (date.indexOf("PM")!=-1)//its pm
{
int pmindex = date.indexOf("PM");
int separator=date.indexOf(":");
int hour = Integer.parseInt(date.substring(0, separator));
int minute = Integer.parseInt(date.substring(separator+1,pmindex));
hour = hour+12;
System.out.println("Time is: " + hour);
}
这篇关于如何在j2me中将12小时转换为24小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!