Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 703
Umakant Patil Avatar asked Aug 08 '11 11:08

Umakant Patil


People also ask

How can I get calendar date selected?

Calendar date = Calendar. getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd"); curDate = sdf. format(date.

How can I get system date in Android?

Date c = Calendar. getInstance(). getTime(); System. out.

What is date format in Android Studio?

DateFormat. format("yyyy-MM-dd hh:mm:ss a", new java.


2 Answers

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);
}
like image 88
Steve Bosman Avatar answered Oct 11 '22 11:10

Steve Bosman


The DateFormat.getDateFormat works as expected. It returns one of three possible formats:

  1. MM/DD/YYYY
  2. DD/MM/YYYY
  3. YYYY/MM/DD

I'm not sure how you set your "Sat, 31 Dec 2011" format, but as far as I know stock Android allows to set only one of three mentioned formats (Settings -> Date and Time -> Select Date Format).

enter image description here

like image 22
inazaruk Avatar answered Oct 11 '22 11:10

inazaruk