How to format date and time in Android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Formatting date and time in Android is mostly about choosing the right API for your minimum SDK and the right format for your users. For modern apps, java.time is the preferred choice, while user-facing display strings should usually respect locale, time zone, and device settings instead of hard-coded patterns.
Core Sections
Prefer java.time for modern Android code
If your app uses Java 8 time APIs through a sufficiently high API level or desugaring, java.time is clearer and safer than SimpleDateFormat.
This works well for machine-like formats or consistent internal display rules. The formatter is immutable and thread-safe, which is a major improvement over older APIs.
Use locale-aware formatting for user display
For UI text shown to end users, fixed patterns are often the wrong default. Android users expect dates and times to match their locale and preferences.
That is usually better than forcing a pattern like MM/dd/yyyy, which can feel wrong or ambiguous outside one region.
Legacy API: SimpleDateFormat
Older Android codebases still use Date and SimpleDateFormat. It works, but be careful: SimpleDateFormat is mutable and not thread-safe.
If you keep this approach, create formatter instances where needed rather than sharing one mutable formatter across threads.
Android-specific display helpers
Sometimes you do not need a custom pattern at all. Android provides utilities that match device preferences more naturally for basic UI formatting.
These helpers are useful for forms, detail screens, and settings pages where the user's local convention matters more than a fixed technical format.
Time zone handling matters more than formatting tokens
A correctly formatted string can still be wrong if it uses the wrong time zone. This is a common bug when server timestamps are stored in UTC but rendered as if they were already local.
If you skip the zone conversion step, users may see a correct format wrapped around the wrong clock time.
Choose format based on purpose
A practical rule:
- logging or API payloads: use stable, explicit formats
- on-screen user display: prefer locale-aware formatting
- date-only and time-only controls: use Android helpers where possible
That keeps the code aligned with the real audience of the string.
Common Pitfalls
- Hard-coding a region-specific pattern for user-facing display when locale-aware formatting would be better.
- Using
SimpleDateFormatas a shared static formatter and running into thread-safety bugs. - Formatting UTC timestamps without converting them to the intended display time zone.
- Mixing old
DateAPIs and newjava.timetypes without a clear conversion boundary. - Assuming a pretty format is enough even when the app needs machine-readable timestamps elsewhere.
Summary
- Use
java.timefor new Android code whenever possible. - Prefer locale-aware formatters for strings shown to users.
- Use
SimpleDateFormatonly when working in older code or compatibility paths, and handle it carefully. - Time zone conversion is just as important as the visible format pattern.
- Match the formatting strategy to the string's purpose: UI, logging, or data exchange.
Related reading
- How to format Joda-Time DateTime to only mm/dd/yyyy?
- How to format strings in Java
- How to free memory in Java?
- How to generate a ddl creation script with a modern Spring Boot Data JPA and Hibernate setup?
- How to format DateTime in Flutter
- How to format DateTime in Flutter
- How to generate a random permutation in Java
- How to generate a secure random alphanumeric string in Java efficiently?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the 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.