如何在Java DateTime API中使用日语数字解析日期字符串

bur*_*ete 8 java datetime internationalization datetime-format java-8

问了[ 如何将日语时代日期字符串值解析为LocalDate和LocalDateTime ]之后,
我对以下情况感到好奇;

?????????????
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以将日语数字解析为日语日历字符(本质上是日语日期)的顶部LocalDate?仅使用Java DateTime API。我不想修改输入的String值,而只想使用API​​来处理识别。

Ole*_*.V. 5

对于任何阅读的人,您的示例日期字符串都带有一个时代标记,即23年(在这种情况下,对应于公元1890年公历),11月和29日。月和日与公历中的相同。

由于日语数字并不完全是位置数字(例如,像阿拉伯数字一样),因此a DateTimeFormatter不能单独解析它们。因此,我们通过提供数字在日语(和中文)中的外观来提供帮助。DateTimeFormatterBuilder有一个重载的appendText方法,该方法接受包含所有可能数字的地图作为文本。我的代码示例不完整,但应该可以帮助您入门。

    Locale japaneseJapan = Locale.forLanguageTag("ja-JP");

    Map<Long, String> numbers = Map.ofEntries(
            Map.entry(1L, "\u4e00"),
            Map.entry(2L, "\u4e8c"),
            Map.entry(3L, "\u4e09"),
            Map.entry(4L, "\u56db"),
            Map.entry(5L, "\u4e94"),
            Map.entry(6L, "\u516d"),
            Map.entry(7L, "\u4e03"),
            Map.entry(8L, "\u516b"),
            Map.entry(9L, "\u4e5d"),
            Map.entry(10L, "\u5341"),
            Map.entry(11L, "\u5341\u4e00"),
            Map.entry(12L, "\u5341\u4e8c"),
            Map.entry(13L, "\u5341\u4e09"),
            Map.entry(14L, "\u5341\u56db"),
            Map.entry(15L, "\u5341\u4e94"),
            Map.entry(16L, "\u5341\u516d"),
            Map.entry(17L, "\u5341\u4e03"),
            Map.entry(18L, "\u5341\u516b"),
            Map.entry(19L, "\u5341\u4e5d"),
            Map.entry(20L, "\u4e8c\u5341"),
            Map.entry(21L, "\u4e8c\u5341\u4e00"),
            Map.entry(22L, "\u4e8c\u5341\u4e8c"),
            Map.entry(23L, "\u4e8c\u5341\u4e09"),
            Map.entry(24L, "\u4e8c\u5341\u56db"),
            Map.entry(25L, "\u4e8c\u5341\u4e94"),
            Map.entry(26L, "\u4e8c\u5341\u516d"),
            Map.entry(27L, "\u4e8c\u5341\u4e03"),
            Map.entry(28L, "\u4e8c\u5341\u516b"),
            Map.entry(29L, "\u4e8c\u5341\u4e5d"),
            Map.entry(30L, "\u4e09\u4e8c\u5341"));

    DateTimeFormatter japaneseformatter = new DateTimeFormatterBuilder()
            .appendPattern("GGGG")
            .appendText(ChronoField.YEAR_OF_ERA, numbers)
            .appendLiteral('\u5e74')
            .appendText(ChronoField.MONTH_OF_YEAR, numbers)
            .appendLiteral('\u6708')
            .appendText(ChronoField.DAY_OF_MONTH, numbers)
            .appendLiteral('\u65e5')
            .toFormatter(japaneseJapan)
            .withChronology(JapaneseChronology.INSTANCE);

    String dateString = "?????????????";
    System.out.println(dateString + " is parsed into " + LocalDate.parse(dateString, japaneseformatter));
Run Code Online (Sandbox Code Playgroud)

该示例的输出为:

????????????? 解析为1890-11-29

假设某个时代可以超过30年,则需要向地图提供更多数字。您可以比我做的更好(并且还可以检查我的错误代码)。使用几个嵌套循环填充地图可能是最好的方法(不太容易出错),但是我不确定我是否可以正确完成操作,因此我将这部分留给您。

今天,我对日语数字有了一些了解。

我使用过的一些链接

  • 我曾经有一次为1-9999范围内的日语数字实现转换例程(双向)。参见[原始代码](https://github.com/MenoData/Time4J/blob/c67e3ce18b080d91330006a8f0a5d96b115b0a47/base/src/main/java/net/time4j/format/NumberSystem.java#L434) (3认同)