使用用户的区域设置格式化日期和时间,包括秒。

huangapple go评论46阅读模式
英文:

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 从[CLDRUnicode通用区域设置数据存储库][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()`接受一个参数用于日期样式另一个参数用于时间样式每个参数可以是shortmediumlong或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规范请求JSR310](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.

huangapple
  • 本文由 发表于 2020年7月23日 01:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63039870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定