如何使用 DateTimeFormatter 解析非标准月份名称

时间:2023-04-19
本文介绍了如何使用 DateTimeFormatter 解析非标准月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要解析以下形式的(德语)日期:

I need to parse (German) dates that come in the following form:

10. Jan. 18:14
8. Feb. 19:02
1. Mär. 19:40
4. Apr. 18:55
2. Mai 21:55
5. Juni 08:25
5. Juli 20:09
1. Aug. 13:42
[...]

如您所见,如果月份的字符数超过 4 个,则月份名称会被删减.更奇怪的是,不要问我为什么,尽管全名是 März,但 3 月份缩短为 Mär..如何用 java.time 解析这个?(日期是根据创建日期列表的 android 设备的本地化格式化的.但是,我不会在 Android 上解析它)

As you can see, the month names are cut if the month has more than 4 characters. Even weirder, don't aks me why, the month of March is shortened to Mär. although the whole name is März. How can I parse this with java.time? (The dates are formatted based on the localization of the android device that creates the list of dates. However, I'm not parsing it on Android)

我的方法是像这样创建一个 DateTimeFormatter:

My approach was to create a DateTimeFormatter like this:

DateTimeFormatter.ofPattern("d. MMMM HH:mm").withLocale(Locale.GERMAN);
// or
DateTimeFormatter.ofPattern("d. MMMMM HH:mm").withLocale(Locale.GERMAN);

但是 MMMMMMMMM 模式都不适合缩短的日期.当然,我可以使用以下模式 d.嗯.HH:mm 来匹配缩短的月份,但是我无法匹配 3 和 4 个字符的月份.我知道我可以有两个格式化程序(MMM. 和 MMMMM),但我宁愿有一个解决方案,我只有一个格式化程序,可能还有一个自定义语言环境或类似的东西.

But neither the MMMM nor the MMMMM pattern fit the dates that are shortened. I can, of course, have the following pattern d. MMM. HH:mm to match the shortened months, but then I can't match the 3 and 4 characters months. I am aware that I can have two formatters (MMM. and MMMMM) but I would rather have a solution where I have only one formatter and possibly a custom locale or something like this.

推荐答案

问题的答案是DateTimeFormatterBuilder 类和 appendText(TemporalField, Map) 方法.它允许任何文本在格式化或解析时与一个值关联,从而有效而优雅地解决问题:

The answer to the problem is the DateTimeFormatterBuilder class and the appendText(TemporalField, Map) method. It allows any text to be associated with a value when formatting or parsing, which solves the problem effectively and elegantly:

Map<Long, String> monthNameMap = new HashMap<>();
monthNameMap.put(1L, "Jan.");
monthNameMap.put(2L, "Feb.");
monthNameMap.put(3L, "Mar.");
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    .appendPattern("d. ")
    .appendText(ChronoField.MONTH_OF_YEAR, monthNameMap)
    .appendPattern(" HH:mm")
    .parseDefaulting(ChronoField.YEAR, 2016)
    .toFormatter();

System.out.println(LocalDateTime.parse("10. Jan. 18:14", fmt));
System.out.println(LocalDateTime.parse("8. Feb. 19:02", fmt));

一些注意事项:

  • monthNameMap 必须填写所有 12 个月
  • 格式化程序通常应分配给静态最终常量,而不是一直创建
  • 已添加 parseDefaulting(YEAR, 2016),以便可以直接使用 LocalDateTime.parse(String, DateTimeFormatter).没有它,就没有年份,因此只能解析 TemporalAccessor (年份必须是闰年,以防解析 2 月 29 日)
  • The monthNameMap must be populated with all 12 months
  • The formatter should normally be assigned to a static final constant, rather than being created all the time
  • The parseDefaulting(YEAR, 2016) has been added so that LocalDateTime.parse(String, DateTimeFormatter) can be used directly. Without it, there would be no year, and thus nothing more than a TemporalAccessor could be parsed (the year must be a leap year, in case 29th Feb is being parsed)

这篇关于如何使用 DateTimeFormatter 解析非标准月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:语言环境货币符号 下一篇:管理 Java 属性文件的本地化

相关文章

最新文章