Android今天的日期

时间:2012-11-01 20:08:34

标签: 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日星期一

2 个答案:

答案 0 :(得分:2)

您应该查看SimpleDateFormat的文档。

SimpleDateFormat

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

应该是

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

答案 1 :(得分: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);