2012-11-01 207 views
0

你好,我想顯示當前的日期。我試圖這樣做:Android今天的日期

calendar c = Calendar.getInstance(); 
System.out.println("Current time => "+c.getTime()); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String formattedDate = df.format(c.getTime()); 
TextView txtView = (TextView)findViewById(R.id.tvDate); 
txtView.setText("Current Date and Time : "+formattedDate); 

但它不顯示在我的textView日期。

有沒有更好的方法來顯示相同​​的日期?我想要這樣:4月22日星期一

+1

如果您在LogCat中顯示formattedDate以驗證它是否正確?還有一個問題,你確定txtView有正確的ID嗎? –

回答

2

你應該看看SimpleDateFormat的文檔。

SimpleDateFormat

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

應該

SimpleDateFormat df = new SimpleDateFormat("EEEE, MMMMM dddd"); 
+0

好的。我知道,但它不顯示我的佈局中的日期 – user1704195

+0

好吧,我解決了這個問題。謝謝 – user1704195

1
Date now = new Date(); 
String str = DateUtils.getRelativeDateTimeString(

     this, // Suppose you are in an activity or other Context subclass 

     now.getTime(), // The time to display 

     MINUTE_IN_MILLIS, // The resolution. This will display only minutes 
          // (no "3 seconds ago" 

     WEEK_IN_MILLIS, // The maximum resolution at which the time will switch 
         // to default date instead of spans. This will not 
         // display "3 weeks ago" but a full date instead 

     0); // Eventual flags 

爲MINUTE_IN_MILLIS和YEAR_IN_MILLIS其他值包括:

SECOND_IN_MILLIS 
MINUTE_IN_MILLIS 
HOUR_IN_MILLIS 
DAY_IN_MILLIS 
WEEK_IN_MILLIS 
YEAR_IN_MILLIS 
Any custom value in milliseconds 

然後設置文本

txtView.setText("Current Date and Time : "+now);