2015-05-25 92 views
-3

如何根據手機設備格式獲取當前時間和日期?獲取設備定義格式的當前時間和日期

我的代碼是

Date date = new Date(System.currentTimeMillis()); 

SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa", 
Locale.ENGLISH); 

String var = dateFormat.format(date)); 

它顯示像2PM而不是14PM。

+1

HH:MM AA變化HH –

+0

@GeorgeThomas - 這是本例中的具體情況,這是在該設備的格式是這樣__wrong__修復'hh'。 –

+0

@ Dev-iL - 那麼我想我們將不得不使用DateFormat.is24HourFormat(上下文) 並相應地應用。 –

回答

1

您可以將設備格式的數據和時間使用

Date date = new Date(); 
    DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); 
    Log.d(TAG,dateFormat.format(date)+"); 
-1

而不是"hh:mm aa"日期格式使用"HH:mm aa"

-1

只是改變「HH:MM AA」爲「HH:MM AA」

0

使用日曆對象,而不是日期,因爲它現在已經過時並通過caledar對象,你可以在12小時內甲as

int hour = calendarObject.get(Calendar.HOUR); 

24甲as獲取時間:

int hour = calendarObject.get(Calendar.HOUR_OF_DAY); 

瞭解更多細節,您可以訪問this