【问题标题】:How to get user-selected date format in Android?如何在 Android 中获取用户选择的日期格式?
【发布时间】:2026-01-11 10:40:01
【问题描述】:
我一直在研究 Android 手机中用户选择的日期格式问题。
我在stackoverlow上经历了几乎很多问题和答案,但还没有得到完美的答案。
例如:-
我在电话中选择的日期格式是“星期六,2011 年 12 月 31 日”
但是当我使用作为在*上回答的基础问题时:-
DateFormat df = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
tv.setText(df.format(date));
以上代码返回 06/08/2011 (mm/dd/yyyy) 格式。
但是请查看我在手机上选择的日期格式。我怎样才能得到确切的格式?
【问题讨论】:
标签:
android
datetime
format
locale
【解决方案1】:
在我目前正在开发的应用程序中,我使用以下内容,我相信它会满足您的要求
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);
}
【解决方案2】:
DateFormat.getDateFormat 按预期工作。它返回三种可能的格式之一:
MM/DD/YYYY
DD/MM/YYYY
YYYY/MM/DD
我不确定您如何设置 “Sat, 31 Dec 2011” 格式,但据我所知,Android 只允许设置上述三种格式中的一种(设置 -> 日期和时间 -> 选择日期格式)。
【解决方案3】:
试试这个
Date inDate = inFormat.parse(in);
DateFormat outFormat = android.text.format.DateFormat.getDateFormat(context);
out=outFormat.format(inDate);