user1624465
user1624465

Reputation:

how to add date and time to a textview in android

I am developing an application where I need to show the date and time in a TextView. How can I implement this?

Do I need to implement the Concept in the XML file or in the java file?

My XML file contains a Textview such as:

<TextView
            android:id="@+id/DATE"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=""
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="30dp" />

What are the steps I need to take?

Guide me.

Upvotes: 5

Views: 17402

Answers (2)

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

here you see display time and date in textview

TextView textView = (TextView) findViewById(R.id.DATE);
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
textView .setText("Time: " + dateFormat.format(date));

Upvotes: 5

endryha
endryha

Reputation: 7256

You should definitely do it in java code.

TextView textView = (TextView) findViewById(R.id.DATE);
textView.setText(DateUtils.formatDateTime(context, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_12HOUR));

Upvotes: 6

Related Questions