2012-11-06 68 views
1

我試圖以像下午7:45而不是19:45格式顯示當前時間,但我似乎無法找到正確的格式選項。標準時間與miltary時間

Time cTime= new Time(Time.getCurrentTimezone()); 
cTime.setToNow(); 
clock.setText(cTime.format("%H:%M")); 

這似乎顯示軍事

+0

嘗試' 「%I:%M%P」' – Squonk

回答

1

您使用Time.format(),所以你可以參考C++ strftime documentation格式化規則。您的格式字符串應爲%I:%M%P,時間爲12小時。

+0

這就是恰到好處,除了它把一個0在面前就像下午8時22 – erik

+0

@erik也就是說Time.format的'限制()',對不起。沒有格式化代碼可以顯示'1-12'格式的小時數。你可以從前面修剪'0'(使用'substring'),或者使用'SimpleDateFormat'。 – Eric

1

使用SimpleDateFormata - 意義AM/PM標記。

例子:

Calendar cal = Calendar.getInstance(); 

SimpleDateFormat dateFormat = new SimpleDateFormat("h:mmaa"); 

try {     
    String now = dateFormat.format(cal.getTime()); 



} catch (ParseException e) { 
    e.printStackTrace(); 
} 
相關問題