本文介绍了日期格式具有特殊模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入的日期类似于 2024年4月17日.日期是由Google语音转文本服务构造的.

I have input date like April 17th, 2024. Date was constructed by google speech to text service.

要将这个日期格式化为其他格式,我将使用下一个代码:

To format this date into different format I would use next code:

        String input = "April 17th, 2024";
        DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d, uuuu" )
                .withLocale( Locale.US );
        ZonedDateTime zdt = ZonedDateTime.parse( input , f );

        LocalDate ld = zdt.toLocalDate();
        DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
        String output = ld.format( fLocalDate);
        System.out.println(output);

...问题是输入日期应为 2024年4月17日或必须改进格式.

...the issue is that input date should be April 17, 2024 or pattern have to be improved.

非常严格的解决方案是在数字后切出字母.

Very strict solution is to cut out letters after number.

我希望避免添加其他逻辑并改善输入数据的日期模式.正是在这一步中,我需要帮助(我在 Java文档).

I would prefer to avoid of adding additional logic and improve date pattern for input data.Exactly with this step I need help (I can't find correct solution in java doc).

因此,我希望得到格式化程序的更正或确认,即Java不支持日期可能为 17th 3rd 等的标准.

So I hope to get formatter correction or confirmation, that java doesn't support standards where date can be 17th, 3rd and so on.

推荐答案

似乎 java.time 不支持序数.

一种模式解决方案".我可以想到的是,它将使用可选部分(即 [] 之间的字符): MMMM d ['th'] ['st'] ['nd'] ['rd'],yyyy

a "pattern solution" I can think of, would be one using optional sections (i.e characters between [ and ]): MMMM d['th']['st']['nd']['rd'], yyyy

例如:

jshell> var fmt = DateTimeFormatter.ofPattern("MMMM d['th']['st']['nd']['rd'], yyyy").withLocale(Locale.US)
fmt ==> Text(MonthOfYear)' 'Value(DayOfMonth)['th']['st'] ... earOfEra,4,19,EXCEEDS_PAD)

jshell> LocalDate.parse("April 17th, 2024", fmt)
$63 ==> 2024-04-17

jshell> LocalDate.parse("April 1st, 2024", fmt)
$64 ==> 2024-04-01

jshell> LocalDate.parse("April 3rd, 2024", fmt)
$65 ==> 2024-04-03

jshell> LocalDate.parse("April 4, 2024", fmt)
$66 ==> 2024-04-04

它将拒绝其他字符:

jshell> LocalDate.parse("April 1fo, 2024", fmt)
|  Exception java.time.format.DateTimeParseException: Text 'April 1fo, 2024' could not be parsed at index 7
|        at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:2046)
|        at DateTimeFormatter.parse (DateTimeFormatter.java:1948)
|        at LocalDate.parse (LocalDate.java:428)
|        at (#68:1)

jshell> LocalDate.parse("April 1baar, 2024", fmt)
|  Exception java.time.format.DateTimeParseException: Text 'April 1baar, 2024' could not be parsed at index 7
|        at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:2046)
|        at DateTimeFormatter.parse (DateTimeFormatter.java:1948)
|        at LocalDate.parse (LocalDate.java:428)
|        at (#69:1)

,但它将接受例如 2024年4月1日,即使并非严格正确:

but it will accept e.g. April 1th, 2024, even if is not strictly correct:

jshell> LocalDate.parse("April 1th, 2024", fmt)
$67 ==> 2024-04-01

,它还将接受 2024年1月3日.我不知道是否可能是一个问题:

and it will also accept January 3thstndrd, 2024. I don't know if could be a problem:

jshell> LocalDate.parse("January 3thstndrd, 2024", fmt)
$97 ==> 2024-01-03

这篇关于日期格式具有特殊模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 04:48