How to parse  Japanese Era Date string values into LocalDate & LocalDateTime

buræquete :

How to parse Japanese era date string inputs into LocalDate/LocalDateTime via Java 8 DateTime API?

Example Japanese calendar dates;

明治23年11月29日
昭和22年5月3日
平成23年3月11日(金)14時46分
令和5年1月11日
buræquete :

It is achieved by utilizing DateTimeFormatter in the following manner;

DateTimeFormatter japaneseEraDtf = DateTimeFormatter.ofPattern("GGGGy年M月d日")
        .withChronology(JapaneseChronology.INSTANCE)
        .withLocale(Locale.JAPAN);

where GGGG in the pattern is the designator for the Japanese characters representing the era
(e.g. 平成 Heisei), and the rest with year/month/day values with their respective Japanese characters: y年 for year, M月 for the month, d日 for the day.

LocalDate.parse("明治23年11月29日", japaneseEraDtf);
LocalDate.parse("昭和22年5月3日", japaneseEraDtf);
LocalDate.parse("令和5年1月11日", japaneseEraDtf);

will give out;

1890-11-29
1947-05-03
2023-01-11

For LocalDateTime, by using the updated pattern "GGGGy年M月d日(E)HH時mm分" in japaneseEraDtf;

LocalDateTime.parse("平成23年3月11日(金)14時46分", japaneseEraDtf);

will result in;

2011-03-11T14:46

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=124424&siteId=1