hh:mm aa(12小时格式)转换为HH:mm(24小时格式) [英] hh:mm aa (12 hour format ) convert in HH:mm (24 hour format)

查看:666
本文介绍了hh:mm aa(12小时格式)转换为HH:mm(24小时格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有String time="02:30 PM"表示12小时格式,我想将此时间转换为24小时格式.

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

我想要这个o/p: 14:30.那么我该如何转换呢?

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

推荐答案

我对浆果一无所知,但是如果API缺少适当的格式化功能,您总是可以自己动手操作字符串本身:

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

这篇关于hh:mm aa(12小时格式)转换为HH:mm(24小时格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆