如何将时间24小时转换为AM/PM并通过time4j删除纳秒和秒?

Rom*_*nov 6 java time java-time time4j

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

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

从24小时格式转换为AM/PM将取决于用户本地化.我想把它(本地化)作为一个论点传播.本地化将看起来像"en-US"字符串.

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

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

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

我必须花很多时间阅读time4j doc,但我的思绪已被吹嘘

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

我必须从我的数据库中获取用户timeZone,这对应于Windows时区并将其转换为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
Run Code Online (Sandbox Code Playgroud)

其中"userTimeZoneId"是来自DB的timeZone.它对应于Microsoft时区名称

我的下一步是从用户时区获取时间/或时间戳,我已将其转换为IANA时区.

我喜欢这样:

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

"winZone"转换时区的地方(例如"WINDOWS~Europe/Kiev")

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

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

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

  3. 如何删除多余的值,如纳秒和秒?

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

Men*_*ild 3

只要您知道 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
Run Code Online (Sandbox Code Playgroud)

如果您想让区域设置控制格式模式,基于样式的解决方案(如上面针对 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
Run Code Online (Sandbox Code Playgroud)

这里不同的大写源于这样一个事实: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
Run Code Online (Sandbox Code Playgroud)