How to convert 12 hours time to 24 hours time in j

2019-07-29 11:45发布

问题:

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