如何在 Android 中获取用户选择的日期格式?

2023-03-20移动开发问题
2

本文介绍了如何在 Android 中获取用户选择的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在研究 Android 手机中用户选择的日期格式问题.我在stackoverlow上经历了几乎很多问题和答案,但还没有得到完美的答案.例如:-我在电话中选择的日期格式是星期六,2011 年 12 月 31 日"但是当我使用作为在stackoverflow上回答的基础问题时:-

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

以上代码返回 06/08/2011 (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);
}

这篇关于如何在 Android 中获取用户选择的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

Sqlite 数据库未从资产文件夹 Android 复制
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)...
2024-04-15 移动开发问题
8

SQLite 数据库副本在由设备而不是模拟器生成时出现损坏
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)...
2024-04-15 移动开发问题
4

安卓文件拷贝
Android file copy(安卓文件拷贝)...
2024-04-15 移动开发问题
6

Android如何在android中检测Edittext的Copy事件
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)...
2024-04-15 移动开发问题
5