how to add date and time to a textview in android

android, android-datepicker

Solution

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

Problem

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.

Original source