我的Android应用希望显示日期时间字符串。它使用SimpleDateFormat,因为它与旧的Android API级别兼容。
Calendar calendar=Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(getTheCurrentLocaleDateTimeFormatString());
localeTimeString= dateFormat.format(calendar.getTimeInMillis());
提供的代码段旨在根据当前的语言环境获取日期时间格式,但我不知道如何将其作为格式字符串获取。它应该由上面的getTheCurrentLocaleDateTimeFormatString()方法计算。
我看到许多格式字符串模式都可用。
这是相关的文档页面:
https://developer.android.com/reference/java/text/SimpleDateFormat#examples
是要以格式显示该格式字符串给用户作为模板,然后将其用作SimpleDateFormat的参数,我的问题是:如何获取当前语言环境的日期时间模式(格式字符串)?
您可以执行以下操作:
SimpleDateFormat getTheCurrentLocaleDateTimeFormatString() {
return (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());
}