英文:
Format date and time with seconds while using user's locale settings
问题
我正在尝试显示包括秒的日期和时间,但格式要根据用户的区域设置。因此,如果某个操作发生在2020年3月5日14:47:51,美国的显示格式应为03/05/2020 14:47:51
,而英国的格式应为05/03/2020 14:47:51
,波兰的格式为05.03.2020, 14:47:51
,捷克的格式为5.3.2020 14:47:51
等。
我能够在不包括秒的情况下显示它:
DateUtils.formatDateTime(
context,
event.time,
DateUtils.FORMAT_24HOUR or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_SHOW_YEAR or DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NUMERIC_DATE
)
但没有常量可以显示秒。有什么建议吗?
英文:
I am trying to display date and time including seconds BUT formatted according to user's locale settings. So if an action happened on the 5th of March 2020 at 14:47:51, in US it should be displayed as 03/05/2020 14:47:51
while in UK it would be 05/03/2020 14:47:51
, in Poland 05.03.2020, 14:47:51
, in Czechia 5.3.2020 14:47:51
etc.
I am able to display it without seconds by:
DateUtils.formatDateTime(
context,
event.time,
DateUtils.FORMAT_24HOUR or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_SHOW_YEAR or DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NUMERIC_DATE
)
But there is no constant to display seconds.
Any ideas, please?
答案1
得分: 3
DateTimeFormatter localizedFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM);
ZonedDateTime dateTime = ZonedDateTime
.of(2020, 3, 5, 14, 47, 51, 123456789, ZoneId.systemDefault());
System.out.println(dateTime.format(localizedFormatter));
我的桌面 Java 11 从[CLDR,Unicode通用区域设置数据存储库][1]获取其区域设置数据,默认情况下使用[自Java 9以来](http://openjdk.java.net/jeps/252)。这些数据在不同区域设置下的输出如下:
en-US 3/5/20, 2:47:51 PM
en-GB 05/03/2020, 14:47:51
pl-PL 05.03.2020, 14:47:51
cs-CZ 05.03.20 14:47:51
在Java 12上实时运行请参见[IdeOne.com][2]。
在Android上的输出可能会有所不同,请自行查看是否满意。
两参数的`DateTimeFormatter.ofLocalizedDateTime()`接受一个参数用于日期样式,另一个参数用于时间样式。每个参数可以是short、medium、long或full。
## 问题:java.time需要Android API级别26吗?
java.time在较旧和较新的Android设备上均表现良好。它只需要至少**Java 6**。
- 在Java 8及更高版本以及较新的Android设备(从API级别26开始),现代API内置。
- 在非Android Java 6和7中,使用ThreeTen Backport,这是现代类的后备(ThreeTen for JSR 310;请参阅底部的链接)。
- 在较旧的Android上,要么使用desugaring,要么使用ThreeTen Backport的Android版。它称为ThreeTenABP。在后一种情况下,请确保从`org.threeten.bp`及其子包中导入日期和时间类。
## 链接
- [Oracle教程:日期时间](https://docs.oracle.com/javase/tutorial/datetime/),解释如何使用java.time。
- [Java规范请求(JSR)310](https://jcp.org/en/jsr/detail?id=310),首次描述了`java.time`。
- [ThreeTen Backport项目](http://www.threeten.org/threetenbp/),将`java.time`后备到Java 6和7(ThreeTen for JSR-310)。
- [通过desugaring可用的Java 8+ API](https://developer.android.com/studio/write/java8-support-table)
- [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP),ThreeTen Backport的Android版本
- [问题:如何在Android项目中使用ThreeTenABP](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project),提供了非常详细的解释。
[1]: https://en.wikipedia.org/wiki/Common_Locale_Data_Repository
[2]: https://ideone.com/N4C0eK
英文:
java.time
DateTimeFormatter localizedFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM);
ZonedDateTime dateTime = ZonedDateTime
.of(2020, 3, 5, 14, 47, 51, 123456789, ZoneId.systemDefault());
System.out.println(dateTime.format(localizedFormatter));
My desktop Java 11 gets its locale data from CLDR, the Unicode Common Locale Data Repository, used by default as of Java 9. These data don’t agree with your expectations in all cases. Output in different locales is:
en-US 3/5/20, 2:47:51 PM
en-GB 05/03/2020, 14:47:51
pl-PL 05.03.2020, 14:47:51
cs-CZ 05.03.20 14:47:51
See this run live on Java 12 at IdeOne.com.
Output on Android may differ, though, please see for yourself if you don’t get satisfactory results.
The two-argument DateTimeFormatter.ofLocalizedDateTime()
takes one argument for date style and one for time style. Each can be short, medium, long or full.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论