首页 > 解决方案 > 如何将时间 24 小时转换为 AM/PM 并通过 time4j 删除纳秒和秒?

问题描述

我面临的问题是我需要将时间从 24 格式转换为 AM/PM 格式(反之亦然),通过 time4j 库删除多余的值,例如纳秒和秒。

我正在使用 time4j 库,因为 Java 无法处理 Windows 时区,我必须通过 time4j 转换它们

从 24 小时格式转换为上午/下午将取决于用户本地化。我想将它(本地化)作为参数传输。本地化看起来像“en-US”字符串。

例如:如果用户本地化是“en-US”,则将 24 小时格式转换为 AM/PM。否则保持当前值。

或者,当我已经定义了用户的本地化时,以我需要的格式获得时间可能会更好?

任何想法如何做到这一点?请帮忙)

我必须花很多时间阅读time4j 文档,但我的想法很震惊

全面了解我正在做的事情以及所有这些的目的:

我必须从我的数据库中获取与 Windows 时区相对应的用户 timeZone 并将它们转换为 IANA 时区。我已经用这种方法做到了

WindowsZone wzn = WindowsZone.of(userTimeZoneId); //userTimeZoneId="eg. FLE Standart Time"
TZID winZone = wzn.resolveSmart(new Locale("","001"));
System.out.println(winZone.canonical()); // WINDOWS~Europe/Kiev

其中“userTimeZoneId”是数据库中的时区。它对应于Microsoft 时区名称

我的下一步是从我已经转换为 IANA 时区的用户时区获取时间/或时间戳。

我确实喜欢这样:

PlainTime currentTime = SystemClock.inZonalView(winZone).now().toTime(); 
//currentTime: "T17:31:37,057"

其中“winZone”转换时区(例如“WINDOWS~Europe/Kiev”)

所以现在回到我在帖子顶部描述的问题。

  1. 当我已经定义了我的用户的本地化时,也许以我需要的格式获得时间会更好?

  2. 如何将时间从 24 格式转换为 AM/PM,反之亦然?

  3. 如何去除纳秒、秒等冗余值?

  4. 如何在转换中使用用户本地化?

标签: javatimejava-timetime4j

解决方案


只要您知道 Time4J 中的专用格式化程序 API 是基于ChronoFormatter的,这就是直截了当的:

Locale ukraine = new Locale("en", "UA"); // or use new Locale("en", "001") for worldwide
TZID winZone = WindowsZone.of("FLE Standard Time").resolveSmart(ukraine);
PlainTime currentTime = SystemClock.inZonalView(winZone).now().toTime();
System.out.println(currentTime); // T12:02:40,344

// truncate seconds and nanoseconds
currentTime = currentTime.with(PlainTime.PRECISION, ClockUnit.MINUTES);
System.out.println(currentTime); // T12:02

// format in am/pm-notation
ChronoFormatter<PlainTime> f1 =
    ChronoFormatter.ofTimePattern("h:mm a", PatternType.CLDR, Locale.US);
String formatted1 = f1.format(currentTime);
System.out.println(formatted1); // 12:02 pm

// or use styled formatter (which has only limited control over displayed precision)
ChronoFormatter<PlainTime> f2 = 
    ChronoFormatter.ofTimeStyle(DisplayMode.SHORT, Locale.US);
String formatted2 = f2.format(currentTime);
System.out.println(formatted2); // 12:02 pm

如果您想让语言环境控制格式模式,则基于样式的解决方案(如上面 Time4J 所示)是合适的。例如,德语语言环境将打印“12:02”而不是“12:02 pm”(美国)。

顺便说一句,java.time如果你愿意,你也可以自由使用 format-API,因为它PlainTime实现了 JSR-310-interface TemporalAccessor

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("h:mm a", Locale.US);
System.out.println(dtf.format(currentTime)); // 12:02 PM

这里不同的大小写源于 JDK(至少在我的系统上)仍然使用旧的 CLDR 数据进行国际化,而 Time4J 有自己的基于实际 CLDR 版本 v33 的资源。未来的 Java 版本肯定会改变大小写。总的来说,我还是建议使用ChronoFormatter更多功能、更好的 i18n 和更高的性能。例如,使用 Time4J 解析 am/pm-literals 的反向方法比java.time使用不同语言环境时更可靠。

如果您喜欢在大写字母(或任何其他自定义格式)中使用“AM”和“PM”,ChronoFormatter那么您还可以使用:

Map<Meridiem, String> map = new EnumMap<>(Meridiem.class);
map.put(Meridiem.AM, "AM");
map.put(Meridiem.PM, "PM");
ChronoFormatter<PlainTime> f3 =
    ChronoFormatter
        .setUp(PlainTime.axis(), Locale.ROOT)
        .addPattern("h:mm ", PatternType.CLDR)
        .addText(PlainTime.AM_PM_OF_DAY, map)
        .build();
String formatted3 = f3.format(currentTime);
System.out.println(formatted3); // 12:02 PM

推荐阅读