gpt4 book ai didi

java - LocalDate 解析抛出异常‽

转载 作者:行者123 更新时间:2023-12-02 09:11:43 25 4
gpt4 key购买 nike

我正在做以下编程练习:Unlucky Days 。声明如下:

Friday 13th or Black Friday is considered as unlucky day. Calculate how many unlucky days are in the given year.

Find the number of Friday 13th in the given year.

Input: Year as an integer.

Output: Number of Black Fridays in the year as an integer.

Examples:

unluckyDays(2015) == 3 unluckyDays(1986) == 1

首先我尝试了以下代码:

import java.time.*;
import java.time.format.*;
import java.util.Locale;

public class Kata {
public static int unluckyDays/*🍀📆*/(int year) {
System.out.println("\nyear: "+year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/dd");
for(int i = 1; i <= 12; i++){
LocalDate date = LocalDate.parse(String.format("%d/%d/13",year,i));
DayOfWeek dow = date.getDayOfWeek();
String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println("\noutput: "+output);
}
return 0;
}
}

我们得到以下异常:

java.time.format.DateTimeParseException: Text '2015/1/13' could not be parsed at index 4
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at java.base/java.time.LocalDate.parse(LocalDate.java:413)
at Kata.unluckyDays(Kata.java:10)

正如我们所见,字符串中索引 4 处的“/”用于 LocalDate.parse(String.format("%d/%d/13",year,i)); 是错误的。

然后我决定更改格式以使用“-”而不是“/”

import java.time.*;
import java.time.format.*;
import java.util.Locale;

public class Kata {
public static int unluckyDays/*🍀📆*/(int year) {
System.out.println("\nyear: "+year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-dd");
for(int i = 1; i <= 12; i++){
LocalDate date = LocalDate.parse(String.format("%d-%d-13",year,i));
DayOfWeek dow = date.getDayOfWeek();
String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println("\noutput: "+output);
}
return 0;
}
}

对于前面的代码,抛出的异常是:

java.time.format.DateTimeParseException: Text '2015-1-13' could not be parsed at index 5
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at java.base/java.time.LocalDate.parse(LocalDate.java:413)
at Kata.unluckyDays(Kata.java:10)

正如我们所见,一位数月份“1”存在困难。

要继续,我将条件用 0 填充那些只有一位数字的月份:

import java.time.*;
import java.time.format.*;
import java.util.Locale;

public class Kata {
public static int unluckyDays/*🍀📆*/(int year) {
System.out.println("\nyear: "+year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-dd");
int count = 0;
for(int i = 1; i <= 12; i++){
LocalDate date = LocalDate.parse(String.format("%d-%s-13",year,String.valueOf(i).length() < 2 ? "0"+i : i));
DayOfWeek dow = date.getDayOfWeek();
String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println("\noutput: "+output);
if(output.equals("Friday")){
count++;
}
}
return count;
}
}

在这个条件下它可以工作。但是,我们如何在 DateTimeFormatter.ofPattern("yyyy-M-dd"); 中指示月份只能有一位数字。

另外,我们应该如何使用不同的日期格式,例如:DateTimeFormatter.ofPattern("dd-M-yyyy");

因为我已经尝试过:

import java.time.*;
import java.time.format.*;
import java.util.Locale;

public class Kata {
public static int unluckyDays/*🍀📆*/(int year) {
System.out.println("\nyear: "+year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-M-yyyy");
int count = 0;
for(int i = 1; i <= 12; i++){
LocalDate date = LocalDate.parse(String.format("13-%s-%d",String.valueOf(i).length() < 2 ? "0"+i : i,year));
DayOfWeek dow = date.getDayOfWeek();
String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println("\noutput: "+output);
if(output.equals("Friday")){
count++;
}
}
return count;
}
}

输出非常有趣:

java.time.format.DateTimeParseException: Text '13-01-2015' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at java.base/java.time.LocalDate.parse(LocalDate.java:413)
at Kata.unluckyDays(Kata.java:11)

据我们观察,连续几天的“13”在索引 0 处导致异常,我不明白。

此外我还读过:

我们怎么知道为什么 LocalDate 解析会抛出异常‽

最佳答案

你离成功很近了。您在这里有两个选择:

  • 使用 DateTimeFormatter 并将其传递给 LocalDate.parse:LocalDate date = LocalDate.parse("13/01/2019", new DateTimeFormatter ("dd/MM/yyyy");
    查看此网站了解更多信息: https://www.baeldung.com/java-string-to-date
  • 或者,您可以使用方法LocalDate.of(year,month,day);:在您的情况下,这将是LocalDate date = LocalDate.of(year, i, 13);。如果您检查 javadoc,您将看到月份参数,1 是一月,12 是十二月。基本上,你可以接受。而且您不需要 DateTimeFormatterString.format...

我已经尝试过你的代码,两种方法都有效。

编辑:
我想回答你的最后一个问题。每次调用 LocalDate.parse("anyStringRepresentingADate") 时,底层发生的情况是 LocalDate 使用默认的 DateTimeFormatter:

public static LocalDate parse(CharSequence text) {
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
}

并且DateTimeFormatter.ISO_LOCAL_DATE'yyyy-MM-dd'
当您执行 LocalDate.parse("13/1/2020") 时,您的字符串与默认格式不匹配,因此出现异常。

关于java - LocalDate 解析抛出异常‽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com