user1704195
user1704195

Reputation: 179

Android Today's date

Hello I want to display current date. I trying do it like this:

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);

But it don't display the date in my textView.

Is there a better way to display the same date? I want like this: Monday, April 22

Upvotes: 0

Views: 571

Answers (2)

Imran Ali
Imran Ali

Reputation: 539

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

Other values for MINUTE_IN_MILLIS and YEAR_IN_MILLIS include:

SECOND_IN_MILLIS
MINUTE_IN_MILLIS
HOUR_IN_MILLIS
DAY_IN_MILLIS
WEEK_IN_MILLIS
YEAR_IN_MILLIS
Any custom value in milliseconds

Then set text as

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

Upvotes: 1

William Melani
William Melani

Reputation: 4268

You should look at the documentation for SimpleDateFormat.

SimpleDateFormat

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

should be

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

Upvotes: 2

Related Questions