Android Development
Date and Time
Android SDK
Mobile App Development
Java Programming

Display the current time and date in an Android application

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Displaying the current date and time in Android is easy if you only need a one-time snapshot. It becomes more interesting when the UI must stay current on screen, respect locale settings, and stop updating when the activity is not visible.

Show the Current Time Once

If the screen only needs a snapshot when it opens or when a button is pressed, format the current moment once and put it in a TextView.

java
1import android.os.Bundle;
2import android.widget.TextView;
3import androidx.appcompat.app.AppCompatActivity;
4import java.text.DateFormat;
5import java.util.Date;
6
7public class MainActivity extends AppCompatActivity {
8    @Override
9    protected void onCreate(Bundle savedInstanceState) {
10        super.onCreate(savedInstanceState);
11
12        TextView textView = new TextView(this);
13        setContentView(textView);
14
15        String now = DateFormat.getDateTimeInstance().format(new Date());
16        textView.setText(now);
17    }
18}

This is enough for simple informational screens.

Update the UI for a Live Clock

If the display should behave like a clock, update it on the main thread at a fixed interval.

java
1import android.os.Bundle;
2import android.os.Handler;
3import android.os.Looper;
4import android.widget.TextView;
5import androidx.appcompat.app.AppCompatActivity;
6import java.text.DateFormat;
7import java.util.Date;
8
9public class MainActivity extends AppCompatActivity {
10    private final Handler handler = new Handler(Looper.getMainLooper());
11    private TextView textView;
12
13    private final Runnable updateClock = new Runnable() {
14        @Override
15        public void run() {
16            String now = DateFormat.getDateTimeInstance().format(new Date());
17            textView.setText(now);
18            handler.postDelayed(this, 1000);
19        }
20    };
21
22    @Override
23    protected void onCreate(Bundle savedInstanceState) {
24        super.onCreate(savedInstanceState);
25        textView = new TextView(this);
26        setContentView(textView);
27    }
28
29    @Override
30    protected void onStart() {
31        super.onStart();
32        handler.post(updateClock);
33    }
34
35    @Override
36    protected void onStop() {
37        super.onStop();
38        handler.removeCallbacks(updateClock);
39    }
40}

Stopping the updates in onStop() matters so the activity does not keep doing unnecessary work when the user is no longer looking at it.

Prefer Locale-Aware Formatting

A fixed pattern like yyyy-MM-dd HH:mm:ss is fine for debugging, but user-facing screens usually benefit from locale-aware formatting.

java
1import java.text.DateFormat;
2import java.util.Date;
3import java.util.Locale;
4
5DateFormat formatter = DateFormat.getDateTimeInstance(
6    DateFormat.MEDIUM,
7    DateFormat.MEDIUM,
8    Locale.getDefault()
9);
10String text = formatter.format(new Date());

This lets Android present the date and time in a style that better matches the user's regional settings.

Use TextClock When a Clock Is All You Need

If the UI only needs a straightforward continuously updating clock, Android already provides TextClock.

xml
1<TextClock xmlns:android="http://schemas.android.com/apk/res/android"
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:format12Hour="h:mm:ss a"
5    android:format24Hour="HH:mm:ss" />

That removes most of the manual update work. It is often the simplest and most maintainable choice for a basic clock display.

Match Update Frequency to Visible Precision

If the UI shows seconds, updating every second makes sense. If it only shows minutes, updating every second is unnecessary work.

This is a small design detail, but it keeps the implementation aligned with what the user can actually see.

Keep UI Updates on the Main Thread

Android views must be updated on the main thread. That is why Handler(Looper.getMainLooper()) appears in the live-clock example.

Even a trivial time display can become buggy if it is updated from the wrong thread.

Common Pitfalls

A common mistake is updating the UI from a background thread instead of the main thread.

Another issue is forgetting to stop repeating updates when the activity is no longer visible, which wastes work and can create lifecycle-related bugs.

Developers also often hardcode a date format for user-facing text when locale-aware formatting would be more appropriate.

Summary

  • Use a TextView and one formatted Date for simple snapshots.
  • Use a main-thread Handler when the screen needs a live updating clock.
  • Remove callbacks when the activity stops.
  • Prefer locale-aware formatting for user-facing text.
  • Use TextClock when you only need a basic continuously updating clock display.

Course illustration
Course illustration

All Rights Reserved.