how to add date and time to a textview in android

16,689

Solution 1

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

Solution 2

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));
Share:
16,689
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    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.