How to add date and time to text view in android

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

Is it necessary to implement the concept in an XML file or in a java file?

My XML file contains a TextView , for example:

 <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 steps should I take?

Guide me

+4
source share
2 answers

You must do this 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)); 
+4
source

here you see the time and date of display in text mode

 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)); 
+3
source

Source: https://habr.com/ru/post/1446746/


All Articles