使用DateTimeFormatter如何解析”Feb 25″和”February 25″?

huangapple go评论62阅读模式
英文:

How do I parse both "Feb 25" and "February 25" using DateTimeFormatter?

问题

我使用以下代码:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMM dd");
LocalDate date = LocalDate.parse(s, formatter);

来解析日期字符串,例如"2019 Feb 25"。但我也希望这段代码可以解析类似"2019 February 25"的字符串。我能否以某种方式更改格式字符串"yyyy MMM dd"以实现这一目标?

我尝试过以下格式字符串:"yyyy MMMM dd"、"yyyy MMM[M] dd"、"yyyy [M+] dd",但我无法使它同时解析"2019 Feb 25"和"2019 February 25"。

英文:

I use the following code:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMM dd");
LocalDate date = LocalDate.parse(s, formatter);

to parse date strings, e.g. "2019 Feb 25". But I also want this code to parse strings like this: "2019 February 25". Can I somehow change the format string "yyyy MMM dd" to achieve this?

I tried the following format strings "yyyy MMMM dd", "yyyy MMM[M] dd", "yyyy [M+] dd", but I can't make it parse both "2019 Feb 25" and "2019 February 25".

答案1

得分: 2

以下是翻译好的内容:

以下代码片段基于建议的答案,证明了可以使用一个解析器来解析不同格式的日期。

然而,不应该将这种解析器用于输出,因为它使用多种格式进行打印。

    // 在解析器中使用多种格式
    DateTimeFormatter parser = DateTimeFormatter.ofPattern(
        "yyyy [MMMM][MMM] dd][yyyy-MM-dd][MM/dd/yyyy]");

    // 为输出准备单一格式
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    LocalDate d1 = LocalDate.parse("2019 Feb 31", parser); // 正确解析到2月的最后一天
    LocalDate d2 = LocalDate.parse("2019 February 26", parser);
    LocalDate d3 = LocalDate.parse("2019-02-27", parser);
    LocalDate d4 = LocalDate.parse("02/30/2020", parser); // 解析为闰年的最后一天

    System.out.println(d1.format(parser));

    System.out.println(d1.format(format));
    System.out.println(d2.format(format));
    System.out.println(d3.format(format));
    System.out.println(d4.format(format));

输出

2019 FebruaryFeb 282019-02-2802/28/2019                                                                                                      
2019-02-28                                                                                                                                          
2019-02-26                                                                                                                                          
2019-02-27                                                                                                                                          
2020-02-29

输出的第一行由parser格式混乱了。

英文:

The following code snippet based on the suggested answer proves that it is possible to parse differently formatted dates using one parser.

However, such parser should not be used for output because it prints using multiple formats.

    // using several formats in parser
    DateTimeFormatter parser = DateTimeFormatter.ofPattern(
        "[yyyy [MMMM][MMM] dd][yyyy-MM-dd][MM/dd/yyyy]");

    // prepare single format for output
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");


    LocalDate d1 = LocalDate.parse ("2019 Feb 31", parser); // parsed correctly to the last Feb day
    LocalDate d2 = LocalDate.parse ("2019 February 26", parser);
    LocalDate d3 = LocalDate.parse ("2019-02-27", parser);
    LocalDate d4 = LocalDate.parse("02/30/2020", parser); // parsed to the last Feb date in a leap year

    System.out.println (d1.format (parser));
      
    System.out.println (d1.format (format));
    System.out.println (d2.format (format));
    System.out.println (d3.format (format));
    System.out.println (d4.format (format));

Output

2019 FebruaryFeb 282019-02-2802/28/2019                                                                                                        
2019-02-28                                                                                                                                             
2019-02-26                                                                                                                                             
2019-02-27                                                                                                                                             
2020-02-29 

The first line of the output is messed up by the parser format.

Online demo at GDB

答案2

得分: 2

如果您指定模式"yyyy [MMMM][MMM] dd",则它会工作:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        String[] dates = new String[]{"2019 January 25", "2019 February 25", "2019 March 25", "2019 April 25",
                "2019 May 25", "2019 June 25", "2019 July 25", "2019 August 25", "2019 September 25", "2019 October 25",
                "2019 November 25", "2019 December 25", "2019 Jan 25", "2019 Feb 25", "2019 Mar 25", "2019 Apr 25",
                "2019 May 25", "2019 Jun 25", "2019 Jul 25", "2019 Aug 25", "2019 Sep 25", "2019 Oct 25", "2019 Nov 25",
                "2019 Dec 25"
        };

        for (String date : dates) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy [MMMM][MMM] dd");
            LocalDate dateMMM = LocalDate.parse(date, formatter);
            System.out.println(dateMMM);
        }
    }
}
英文:

If you specify the pattern "yyyy [MMMM][MMM] dd" then it works:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        String[] dates = new String[]{"2019 January 25", "2019 February 25", "2019 March 25", "2019 April 25",
                "2019 May 25", "2019 June 25", "2019 July 25", "2019 August 25", "2019 September 25", "2019 October 25",
                "2019 November 25", "2019 December 25", "2019 Jan 25", "2019 Feb 25", "2019 Mar 25", "2019 Apr 25",
                "2019 May 25", "2019 Jun 25", "2019 Jul 25", "2019 Aug 25", "2019 Sep 25", "2019 Oct 25", "2019 Nov 25",
                "2019 Dec 25"
        };

        for (String date : dates) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy [MMMM][MMM] dd");
            LocalDate dateMMM = LocalDate.parse(date, formatter);
            System.out.println(dateMMM);
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月8日 21:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定