hh:mm aa (12 hour format ) convert in HH:mm (24 hour format)

。_饼干妹妹 提交于 2020-01-06 01:50:09

问题


I have String time="02:30 PM" means 12 hour format and i want to convert this time in 24 hour format .

I want this o/p: 14:30. so how can i convert this ?


回答1:


I don't know anything about berries, but in case the API is missing a proper formatting function, you can always get your hands dirty with the string itself:

static String convert(String time){

    boolean pm = "PM".equals(time.substring(6).toUpperCase());
    int h = Integer.valueOf(time.substring(0,2));

    if (h!=12)
        h+=pm?12:0;
    else
        h=pm?h:0;
    return ((h<10)?"0":"")+h + ":" + time.substring(3,5);
}



回答2:


Try this code.

This will give the current time in 24 Hours format.

    SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
    String newTime = formate.formatLocal(System.currentTimeMillis());


来源:https://stackoverflow.com/questions/8091235/hhmm-aa-12-hour-format-convert-in-hhmm-24-hour-format

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