我正在处理一个问题集,并尝试构建一个返回日期的类。对于这个问题,我需要返回星期几,例如给定日期的“星期一”,“星期二”。我搜索了Stack Overflow,并决定尝试使用java.time:
package Chapter1.Section2;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class exercise_1_2_12 {
private int day;
private int month;
private int year;
public exercise_1_2_12(int month, int day, int year){
if (!isDateValid(month, day, year)){
throw new IllegalArgumentException("Invalid Date!");
}
this.month = month;
this.day = day;
this.year = year;
}
public int day(){
return day;
}
public int month(){
return month;
}
public int year(){
return year;
}
public String toString(){
//return month() + "-" + day() + "-" + year();
return year + "/" + String.format("%02d", month) + "/" +
String.format("%02d", day);
}
public boolean isDateValid(int month, int day, int year) {
int[] dayInEachMonthLeapYr = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] dayInEachMonthNonLeapYr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean valid = true;
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
if (year < 1 || month < 1 || month > 12 || day < 1 || day > dayInEachMonthLeapYr[month - 1]) {
valid = false;
}
} else {
if (year < 1 || month < 1 || month > 12 || day < 1 || day > dayInEachMonthNonLeapYr[month - 1]) {
valid = false;
}
}
return valid;
}
public String dayOfWeekName(){
DateTimeFormatter format = DateTimeFormatter.ofPattern("mm/dd/yyyy");
String input = this.toString();
LocalDate date = LocalDate.parse(input, format);
DayOfWeek dayOfWeek = date.getDayOfWeek();
String result = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US);
return result;
}
public static void main(String[] args){
StdOut.print("Enter month: ");
int month = StdIn.readInt();
StdOut.print("Enter day: ");
int day = StdIn.readInt();
StdOut.print("Enter year: ");
int year = StdIn.readInt();
exercise_1_2_12 smartDate = new exercise_1_2_12(month,day,year);
StdOut.println(smartDate);
smartDate.dayOfWeekName();
}
}
问题可能出在实例方法dayOfWeekName()中,我试图解析这个对象。到String格式,然后将其存储在LocalDate日期,然后返回工作日的名称。我测试了它,错误信息如下:
Enter month: 4
Enter day: 1
Enter year: 2018
2018/04/01
Exception in thread "main" java.time.format.DateTimeParseException: Text '2018/04/01' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Chapter1.Section2.exercise_1_2_12.dayOfWeekName(exercise_1_2_12.java:64)
at Chapter1.Section2.exercise_1_2_12.main(exercise_1_2_12.java:85)
Process finished with exit code 1
我不太确定如何解决这个问题。
答案 0 :(得分:2)
查看您的日期模式:
DateTimeFormatter format = DateTimeFormatter.ofPattern("mm/dd/yyyy");
日期:
'2018/04/01'
自然不匹配。
在该模式中输入日期,例如:
'04/01/2018'
或更改模式:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
重要提示:正如评论中@Ivar精确指出的那样,请注意the pattern for months is uppercase M
。当您使用mm
时,它会匹配分钟。
答案 1 :(得分:-1)
使用此格式化程序:
DateTimeFormatter.ofPattern("MM/dd/yyyy")