How to convert 12 hours time to 24 hours time in j2me?

你说的曾经没有我的故事 提交于 2019-12-24 16:27:24

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/9531582/how-to-convert-12-hours-time-to-24-hours-time-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!