如何获得用户选择的日期格式的Andr​​oid?如何获得、日期、格式、用户

2023-09-04 06:54:23 作者:撒娇小小怪

我一直在努力通过用户在Android手机选定的日期格式问题。 我已经经历了几乎很多的问题和答案了对stackoverlow和还没有得到完美的答案。 例如:- 我选择的日期格式的手机是星期六,2011年12月31 但是,当我作为基础的问题回答了计算器: -

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));

以上code返回我2011年6月8日(MM / DD / YYYY)的格式。 但见我选择手机上的日期格式。我怎样才能得到确切的格式?

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?

推荐答案

在一个应用程序,我目前正在开发我用下面的,我相信会做什么,你需要

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);
}