Sverre Rabbelier
Sverre Rabbelier

Reputation: 1496

Display date AND time in user locale

I know I can use android.text.format.DateFormat.getDateFormat() to format my dates, and android.text.format.DateFormat.getTimeFormat to format my times, but how do I format a datetime? Similar to the getDateTimeInstance method from java.text.DateFormat.

I'm currently just concatenating the result of both the getDateFormat and getTimeFormat's formatters, but I don't know which way around the user prefers to have their dates and times shown.

Upvotes: 17

Views: 14753

Answers (2)

Loxley
Loxley

Reputation: 1781

Do you need the String representation only? If yes, please have a look at
DateUtils.formatDateTime(Context context, long millis, int flags):

public static String formatDateTime (Context context, long millis, int flags)

in: android.text.format.DateUtils

Upvotes: 29

Maragues
Maragues

Reputation: 38324

To extend the answer, here's a functional example

DateUtils.formatDateTime(context, epochTimeInMs, DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_DATE);

This would print Apr 8 for a US Locale, or 8 de abr for a Spanish Locale

You can see the full list of valid flags at DateUtils documentation

Upvotes: 11

Related Questions