Android Development
Date Formatting
Time Formatting
Android Studio
Java Programming

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.

Browse interview questions

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.

kotlin
1import java.time.LocalDateTime
2import java.time.format.DateTimeFormatter
3import java.util.Locale
4
5val now = LocalDateTime.now()
6val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm", Locale.US)
7val text = now.format(formatter)
8
9println(text)

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.

kotlin
1import java.time.ZonedDateTime
2import java.time.format.FormatStyle
3import java.time.format.DateTimeFormatter
4import java.util.Locale
5
6val timestamp = ZonedDateTime.now()
7val formatter = DateTimeFormatter
8    .ofLocalizedDateTime(FormatStyle.MEDIUM)
9    .withLocale(Locale.getDefault())
10
11val displayText = timestamp.format(formatter)

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.

java
1import java.text.SimpleDateFormat;
2import java.util.Date;
3import java.util.Locale;
4
5Date now = new Date();
6SimpleDateFormat formatter =
7    new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
8
9String text = formatter.format(now);
10System.out.println(text);

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.

java
1import android.text.format.DateFormat;
2import java.util.Date;
3
4Date now = new Date();
5CharSequence dateText = DateFormat.getMediumDateFormat(context).format(now);
6CharSequence timeText = DateFormat.getTimeFormat(context).format(now);

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.

kotlin
1import java.time.Instant
2import java.time.ZoneId
3import java.time.format.DateTimeFormatter
4
5val instant = Instant.parse("2026-03-07T18:30:00Z")
6val localText = instant
7    .atZone(ZoneId.systemDefault())
8    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))

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 SimpleDateFormat as a shared static formatter and running into thread-safety bugs.
  • Formatting UTC timestamps without converting them to the intended display time zone.
  • Mixing old Date APIs and new java.time types without a clear conversion boundary.
  • Assuming a pretty format is enough even when the app needs machine-readable timestamps elsewhere.

Summary

  • Use java.time for new Android code whenever possible.
  • Prefer locale-aware formatters for strings shown to users.
  • Use SimpleDateFormat only 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.