Skip to content

Localized Date/Time in Android

Custom localized date format with DateUtils.formatDateTime()

Section titled “Custom localized date format with DateUtils.formatDateTime()”

DateUtils.formatDateTime() allows you to supply a time, and based on the flags you provide, it creates a localized datetime string. The flags allow you to specify whether to include specific elements (like the weekday).

formatDateTime() automatically takes care about proper date formats.

Format a date:

Format a date and time. Date is in short format, time is in long format:

Commonly used patterns:

  • HH: hour (0-23)
  • hh: hour (1-12)
  • a: AM/PM marker
  • mm: minute (0-59)
  • ss: second
  • dd: day in month (1-31)
  • MM: month
  • yyyy: year

It is recommended to use methods of the DateUtils class in order to format dates which are locale aware, i.e. which consider user preferences (e.g. 12h/24h clock time formats). These methods are most appropriate for dates that are displayed to the user.

For fully customized date representations, it is recommended to use the SimpleDateFormat class, as it allows to fully control all date elements.