How to get current time and date in Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In modern Android code, the current date and time should usually come from the java.time API, not from the older Date and Calendar classes. The right class depends on what you actually need: an absolute timestamp, a local calendar date, or a formatted value for display.
Use java.time for New Android Code
Android's current guidance and API references support java.time, and core library desugaring makes a substantial subset available on older Android versions as well. That means new application code can usually start with the same date-time types you would choose on the JVM more generally.
If you need the current moment on the timeline, use Instant.now():
This is the best representation for logging, server communication, and stored timestamps because it is not tied to the user's local time zone.
Pick the Type That Matches the Job
java.time is easier to use than legacy APIs partly because each type represents one concept clearly.
- '
Instantis an absolute point in time.' - '
LocalDateis a calendar date with no clock time.' - '
LocalTimeis a clock time with no date.' - '
LocalDateTimeis a date and time with no zone.' - '
ZonedDateTimeis a date and time in a specific zone.'
If the app only needs today's date on the device, LocalDate.now() is more honest than Instant.
If the app needs the current device-local wall clock time with zone awareness, use ZonedDateTime.now():
Choosing the narrowest correct type prevents later confusion about whether a value is suitable for storage, display, or arithmetic.
Format for Display with DateTimeFormatter
Once you have the current value, formatting is the next step. DateTimeFormatter gives you explicit control and also supports localized formats.
This is often better than hard-coding a pattern because it respects the user's locale conventions. If the product requires a fixed format, a pattern-based formatter is still fine:
Treat Time Zones as a First-Class Decision
The most common date-time bug is using local time where an absolute timestamp was required. If your app stores values in a database or sends them to a server, capture an Instant and convert to a display zone only at the UI boundary.
This keeps the stored value stable while still allowing local presentation later.
Legacy APIs Still Exist, but Use Them Only When Required
You will still see older Android examples based on Calendar.getInstance() or Date(). Those APIs are not broken, but they are more mutable and less expressive.
Use that style only when maintaining old code or integrating with an API that already depends on it. New code is easier to read and test with java.time.
Keep Retrieval Separate from UI Refresh
Getting the current time is cheap. The more important design question is how often the UI should update and who owns that schedule. A clock label, countdown, or status timestamp should separate time acquisition from the UI refresh mechanism.
For example, the current time can come from Instant.now() or ZonedDateTime.now(), while a coroutine, Handler, or lifecycle-aware state holder controls when the screen redraws. That separation keeps your date-time logic from getting tangled up with rendering behavior.
Common Pitfalls
- Using
LocalDateTimefor values that really need an absolute timestamp or zone information. - Reaching for
Calendarin new code out of habit. - Formatting local device time for storage or API exchange without capturing the zone or offset.
- Hard-coding display formats where localized formatting would be better for users.
- Mixing time retrieval logic with UI scheduling logic in one hard-to-test code path.
Summary
- Prefer
java.timein modern Android code. - Use
Instantfor absolute timestamps andLocalDateorZonedDateTimefor local values as needed. - Format values with
DateTimeFormatter, preferably localized when appropriate. - Store absolute time and convert to local time for display.
- Use legacy
Calendaronly when you are forced to maintain older code.
Related reading
- How to get current time and date in Android
- How to get device make and model on iOS?
- How to get enum from raw value in Swift?
- How to get hosting Activity from a view?
- How to get input value from a UIAlertController text field?
- How to get input value from a UIAlertController text field?
- How to get iOS device's WiFi IP address?
- How to get Latitude and Longitude of the mobile device in android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.