how to add date and time to a textview in android

user1624465 picture user1624465 · Nov 18, 2012 · Viewed 16.3k times · Source

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.

Answer

endryha picture endryha · Nov 18, 2012

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