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.
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.
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.
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.
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
TextViewand one formattedDatefor simple snapshots. - Use a main-thread
Handlerwhen the screen needs a live updating clock. - Remove callbacks when the activity stops.
- Prefer locale-aware formatting for user-facing text.
- Use
TextClockwhen you only need a basic continuously updating clock display.

