获得本地化的短日期模式为String? [英] Get localized short date pattern as String?

查看:148
本文介绍了获得本地化的短日期模式为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆