2011-11-11 379 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()); 
相關問題