获得本地化的短日期模式为String? [英] Get localized short date pattern as String?
问题描述
我想获得一个短日期为字符串
被定制为用户的语言环境的模式(例如E DD MMM)。我可以很容易地得到一个本地化的日期格式
-object使用结果日期格式mDateFormat = android.text.format.DateFormat.getDateFormat(mContext);
结果
但日期格式
不具有 .toPattern()
-method。
I'd like to get the pattern of a "short date" as a String
that is customized to the user's locale (e.g. "E dd MMM"). I can easily get a localized DateFormat
-object using
DateFormat mDateFormat = android.text.format.DateFormat.getDateFormat(mContext);
but DateFormat
doesn't have a .toPattern()
-method.
如果我用
的SimpleDateFormat自卫队=新的SimpleDateFormat();结果
字符串mDateFormatString = sdf.toPattern();
我不知道如何只获取的短的日期字符串的模式,而不是完整 M / D / YY H:毫米
I don't know how to only get the short date pattern String instead of the full M/d/yy h:mm a
推荐答案
记住日期格式
为基类的SimpleDateFormat
所以你可以随时施放它,抓住格局。
remember DateFormat
is the base class for SimpleDateFormat
so you can always cast it and grab the pattern.
final DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
// getDateFormat() returns a SimpleDateFormat from which we can extract the pattern
if (shortDateFormat instanceof SimpleDateFormat) {
final String pattern = ((SimpleDateFormat) shortDateFormat).toPattern();
这篇关于获得本地化的短日期模式为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!