Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I get a locale-based short date string for Android?

I'm trying to get a date string with the shortened form of the day of week and month, in addition to the day of month.

For example, a user with an English locale would see:

Tue Jun 17

and a user with a German locale would see:

Di. 17 Juni

I've been looking at the android.text.format.DateFormat docs and the getBestDateTimePattern(Locale locale, String skeleton) looked like it might work, but it requires API 18+, so I can't use it.

Is there a way to get this kind of short format that is based on the user's current locale?

like image 973
JDJ Avatar asked Jan 21 '26 10:01

JDJ


1 Answers

Okay, I think I finally figured this out:

int flags = DateUtils.FORMAT_SHOW_DATE | 
            DateUtils.FORMAT_NO_YEAR | 
            DateUtils.FORMAT_ABBREV_ALL | 
            DateUtils.FORMAT_SHOW_WEEKDAY;

dateTextView.setText(DateUtils.formatDateTime(this, millis, flags));

For an English locale, you get:

Wed, Jun 18

and for a German locale, you get:

Mi., 18. Juni

and for a French locale, you get:

mer. 18 juin

like image 191
JDJ Avatar answered Jan 24 '26 03:01

JDJ