2011-08-14 29 views
4

我想要取得日期。如何以正確格式獲取完整日期?

我使用這個返回的INT我希望它是一個字符串的日期和月份

int date = cld.get(Calendar.DATE); 
int month = cld.get(Calendar.MONTH); 

代替它。或者可能是全日期格式。

如:12年12月12日 或...

2011年8月14日

我怎麼會去這樣做?

回答

4

您想了解SimpleDateFormat

獲取當前時間ISO8601格式的基礎:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ"); 
String now = df.format(new Date()); 

對於其它的格式:

DateFormat df = new SimpleDateFormat("MMM d, yyyy"); 
String now = df.format(new Date()); 

DateFormat df = new SimpleDateFormat("MM/dd/yy"); 
String now = df.format(new Date()); 
+0

謝謝,我知道的SimpleDateFormat。只是沒有想到使用它非常感謝 –

5

請使用

機器人。 text.fo rmat.DateFormat.getDateFormat(Context context) android.text.format.DateFormat.getTimeFormat(Context context)

從當前用戶設置(例如12/24時間格式)獲取有效的時間和日期格式, 。

import android.text.format.DateFormat; 

private void some() { 
    final Calendar t = Calendar.getInstance(); 
    textView.setText(DateFormat.getTimeFormat(this/*Context*/).format(t.getTime())); 
} 
1

您可以獲取日期爲你的願望如下

Date date = new Date(); 
String string = (String) DateFormat.format("yyyy-MM-dd hh:mm:ss", date.getTime()); 

您可以替換這個(YYYY-MM-DD HH:MM:SS)作爲你的願望,例如(YY-MM-DD HH:MM)

0

你可以在一個線路全程日期:

String string = (String) DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date().getTime()); 

你可以使用這個鏈接更多格式: