Localized Date/Time in Android
Remarks#
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.
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).
Date date = new Date();
String localizedDate = DateUtils.formatDateTime(context, date.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY);
formatDateTime() automatically takes care about proper date formats.
Standard date/time formatting in Android
Format a date:
Date date = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String localizedDate = df.format(date)
Format a date and time. Date is in short format, time is in long format:
Date date = new Date();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
String localizedDate = df.format(date)
Fully customized date/time
Date date = new Date();
df = new SimpleDateFormat("HH:mm", Locale.US);
String localizedDate = df.format(date)
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