Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:59:45+00:00 2026-05-23T06:59:45+00:00

I want to display the time and date in a TextView in real time

  • 0

I want to display the time and date in a TextView in real time (updating it by the minute). Currently, I have this. Is this the best way of doing that, considering memory use and Android best practice? (note: DateFormat is java.text.DateFormat)

private Thread dtThread;

public void onCreate(Bundle savedInstanceState) {
    ...
    getDateAndTime();
}

private void getDateAndTime() {
    dtThread = new Thread( new Runnable() {

        @Override
        public void run() {
            Log.d(TAG, "D/T thread started");
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    update();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.d(TAG, "D/T thread interrupted");
                }
            }
        }

        public void update() {
            runOnUiThread( new Runnable() {

                @Override
                public void run() {
                    Date d = new Date();
                    String time = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(d);
                    String date = DateFormat.getDateInstance(DateFormat.LONG).format(d);

                    TextView timeView = (TextView) findViewById(R.id.textStartTime);
                    TextView dateView = (TextView) findViewById(R.id.textStartDate);
                    timeView.setText(time);
                    dateView.setText(date);
                }

            });
        }

    });

    dtThread.start();
}

protected void onPause() {
    super.onPause();
    dtThread.interrupt();
    dtThread = null;
}

protected void onResume() {
    super.onResume();
    getDateAndTime();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:59:46+00:00Added an answer on May 23, 2026 at 6:59 am

    I would use a Runnable and post it with a delay to a Handler.

    public class ClockActivity extends Activity {
    
        private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    
        private TextView mClock;
        private boolean mActive;
        private final Handler mHandler;
    
        private final Runnable mRunnable = new Runnable() {
            public void run() {
                if (mActive) {
                    if (mClock != null) {
                        mClock.setText(getTime());
                    }
                    mHandler.postDelayed(mRunnable, 1000);
                }
            }
        };
    
        public ClockActivity() {
            mHandler = new Handler();
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mClock = (TextView) findViewById(R.id.clock_textview);
            startClock();
        }
    
        private String getTime() {
            return sdf.format(new Date(System.currentTimeMillis()));
        }
    
        private void startClock() {
            mActive = true;
            mHandler.post(mRunnable);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display a date/time submitted value much the same way as Stack
I want to display date time like 01/08/11 , but i write this string
I want to display the time and date of updated row from MySQL to
I want to display the username/last connection date/time and some other info on all
I want to display time on my page in real tics format like running
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
i'm using jqplot 1.0.0b1_r746. i want to display some time periods using the date
In my app I want the time/date to display as Month/Year (e.g. 7/10). The
Hello I want to display current date. I trying do it like this: calendar
I want to display date and time in the top of the magento website

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.