簡體   English   中英

Java LocalDate 解析中的不一致行為

[英]Inconsistent Behaviour in Java LocalDate parse

當我解析無效日期“29/02/2007”時,我需要解析一個格式為 dd/MM/yyyy 的日期,它作為 LocalDate 2007-02-28 返回給我,代碼如下:

        LocalDate birthD = null;
    
    try {
        birthD = LocalDate.parse("29/02/2007", DateTimeFormatter.ofPattern("dd/MM/yyyy"));
    } catch (DateTimeParseException e) {
        e.printStackTrace();
    }


    System.out.println("birthDStr: " + birthDStr);
    System.out.println("parsed birthD: " + birthD);

但是,如果我使用 ISO 格式(不帶 DateTimeFormatter)進行解析,則會出現異常,代碼如下:

        final String birthDStr = "2007-02-29";
    LocalDate birthD = null;
    
    try {
        birthD = LocalDate.parse(birthDStr);
    } catch (DateTimeParseException e) {
        e.printStackTrace();
    }


    System.out.println("birthDStr: " + birthDStr);
    System.out.println("parsed birthD: " + birthD);

例外:

java.time.format.DateTimeParseException: Text '2007-02-29' could not be parsed: Invalid date 'February 29' as '2007' is not a leap year
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDate.parse(LocalDate.java:400)
at java.time.LocalDate.parse(LocalDate.java:385)
at com.cchaidoris.quick_test.DateTest.parseTest2(DateTest.java:65)
at com.cchaidoris.quick_test.DateTest.main(DateTest.java:41)
Caused by: java.time.DateTimeException: Invalid date 'February 29' as '2007' is not a leap year
    at java.time.LocalDate.create(LocalDate.java:429)
    at java.time.LocalDate.of(LocalDate.java:269)
    at java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:560)
    at java.time.chrono.IsoChronology.resolveYMD(IsoChronology.java:123)
    at java.time.chrono.AbstractChronology.resolveDate(AbstractChronology.java:472)
    at java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:492)
    at java.time.chrono.IsoChronology.resolveDate(IsoChronology.java:123)
    at java.time.format.Parsed.resolveDateFields(Parsed.java:351)
    at java.time.format.Parsed.resolveFields(Parsed.java:257)
    at java.time.format.Parsed.resolve(Parsed.java:244)
    at java.time.format.DateTimeParseContext.toResolved(DateTimeParseContext.java:331)
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1955)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)

所以我的問題是,我想考慮一下:

LocalDate.parse("29/02/2007", DateTimeFormatter.ofPattern("dd/MM/yyyy"));
 

無效,我該如何使用 LocalDate.parse?

您需要更改解析器樣式

DateTimeFormatter formatter = DateTimeFormatter
    .ofPattern("dd/MM/uuuu")
    .withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse("29/02/2007", formatter); // Throws

請注意使用uuuu而不是yyyy來指定年份而不是時代年份。 (我確定有某種方法可以讓yyyy工作並假設一個 AD 時代,但我不確定如何副手。)

請注意, LocalDate.parse (沒有格式化程序)被記錄為使用DateTimeFormatter.ISO_LOCAL_DATE ,它使用 STRICT 解析器 - 所以這里並沒有真正的不一致。

將其用於測試。

@Test
void test() {

    final String birthDStr = "2007-02-27";
    LocalDate birthD = null;

    try {
        birthD = LocalDate.parse(birthDStr);
    } catch (DateTimeParseException e) {
        e.printStackTrace();
    }

}


@Test
void test() {

    final String birthDStr = "29/02/2007";
    LocalDate birthD = null;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

    try {
        birthD = LocalDate.parse(birthDStr, formatter);
    } catch (DateTimeParseException e) {
        e.printStackTrace();
    }

}

您的代碼正確,但日期不正確。 無法解析文本“2007-02-29”:日期“2 月 29 日”無效,因為“2007”不是閏年。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM