使用 getBestDateTimePattern()

And*_*son 4 android

据有关Android 4.3的消息

\n\n
\n

为了帮助您管理跨区域设置的日期格式,Android\n 4.3 包含一个新的getBestDateTimePattern()方法,该方法可自动为您指定的区域设置生成最佳可能的本地化形式的 Unicode UTS 日期。它\xe2\x80\x99 是为用户提供更加本地化的体验的便捷方式。

\n
\n\n

我该如何使用这个方法?我找不到。

\n

Pat*_*Lee 6

7\xc2\xbd 年后,这是一个带有实际示例的答案!;-)

\n
Locale locale = Locale.getDefault();\nString skeleton = DateFormat.getBestDateTimePattern(locale, "MMMM d, YYYY");\n
Run Code Online (Sandbox Code Playgroud)\n

然后,使用 java.time:

\n
Instant.now().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(skeleton));\n
Run Code Online (Sandbox Code Playgroud)\n

或者使用遗留类:

\n
Date now = Calendar.getInstance().getTime();\nSimpleDateFormat dateTimeFormatter = new SimpleDateFormat(skeleton, locale);\ndateTimeFormatter.setTimeZone(TimeZone.getDefault());\ndateTimeFormatter.applyLocalizedPattern(skeleton);\ndateTimeFormatter.format(now);\n
Run Code Online (Sandbox Code Playgroud)\n

  • https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns (2认同)