2011-11-11 377 views
1

我有String time="02:30 PM"表示12小时制格式,我想将此时间转换为24小时格式。hh:mm aa(12小时格式)转换为HH:mm(24小时制)

我想要这个o/p: 14:30。所以我该如何转换呢?

+0

标签只有黑莓让人们只把黑莓的代码。 –

+0

在第一个发送的问题也有Java标记后有时Java标记删除 –

+0

请参阅重复:http://stackoverflow.com/questions/8090196/yyyy-mm-dd-and-hhmm-pm-am-convert-into-long –

回答

4

我不知道什么浆果,但万一API缺少适当的格式化功能,你总是可以得到你的手脏与字符串本身:

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

试试看看这个代码。

这将以24小时格式给出当前时间。

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