Search code examples
androiddatetimeformatlocale

How to get user-selected date format in Android?


I have been working on problem of date format selected by users in the Android Phone. I have gone through almost many question and answers on the stackoverlow and havent got the perfect answer. For example:- I have selected dateformat in phone is "Sat, 31 Dec 2011" But when I used as based question answered on stackoverflow:-

DateFormat df = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
tv.setText(df.format(date));

Above code returns me 06/08/2011 (mm/dd/yyyy) format. But see the date format i selected on phone. How can i get the exact format?


Solution

  • In an application I'm currently developing I use the following, which I believe will do what you require

    final String format = Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);
    if (TextUtils.isEmpty(format)) {
      dateFormat = android.text.format.DateFormat.getMediumDateFormat(getApplicationContext());
    } else {
      dateFormat = new SimpleDateFormat(format);
    }