gpt4 book ai didi

java - DateTimeFormatter 中的 IllegalArgumentException

转载 作者:行者123 更新时间:2023-12-02 05:27:38 25 4
gpt4 key购买 nike

这是我的异常(exception):

java.lang.IllegalArgumentException: Invalid format: "2014-09-16" is malformed at "-09-16"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
at Project.BorrowedModel.getDateDifference(BorrowedModel.java:216)
at Project.BorrowedModel.UserHasLatedReturn(BorrowedModel.java:195)
...

这是我的方法:

(此代码用于检测我们图书馆的用户是否有 10 天内未归还的图书)。

public boolean UserHasLatedReturn(int userID) throws NullPointerException {
String todayDate = getTodayDate();
String userBorrowDate = getUserBorrowDate(userID);
if (userBorrowDate == null) {
return false;
}
int difference = getDateDifference(userBorrowDate, todayDate);
if (difference > 10) { // More that 10 days
System.out.println("You have " + difference + " Days Delay in returning your previouse book");
return true;
}
return false;
}

public int getDateDifference(String firstDate, String secondDate) {

DateTime d1, d2, dt1 = null, dt2 = null;
DateTimeFormatter format = DateTimeFormat.forPattern("YYYY/MM/dd");

try {
d1 = format.parseDateTime(firstDate); // Exception is here
d2 = format.parseDateTime(secondDate);

dt1 = new DateTime(d1);
dt2 = new DateTime(d2);

} catch (Exception e) {
e.printStackTrace();
}
return Days.daysBetween(dt1, dt2).getDays();
}

public String getTodayDate() {
Calendar todayDate = Calendar.getInstance();
SimpleDateFormat simpleFormat = new SimpleDateFormat("YYYY/MM/dd");
String strDate = simpleFormat.format(todayDate.getTime());
return strDate;
}

public String getUserBorrowDate(int userID) {
String query = "SELECT Borrow_Date FROM Borrowed WHERE User_ID=?";
String date = null;
try (Connection con = DriverManager.getConnection(dbUrl, "root", "2323");
PreparedStatement ps = con.prepareStatement(query);) {
ps.setInt(1, userID);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
date = rs.getString("Borrow_Date");
}
} catch (Exception e) {
e.printStackTrace();
}
return date;
}

这段代码之前可以正常工作,但现在不行!

我的代码有什么问题吗?

最佳答案

正如其他人所指出的,您从 getUserBorrowDate() 获取格式错误的日期字符串,并将其最终传递给 getDateDifference() ,这会引发异常。我只需使用 ResultSet.getDate() 并比较日期,而不是进行所有字符串解析。

public boolean UserHasLatedReturn(int userID) throws NullPointerException {
LocalDate todayDate = new LocalDate();
LocalDate userBorrowDate = getUserBorrowDate(userID);
if (userBorrowDate == null) {
return false;
}
int difference = Days.daysBetween(userBorrowDate, todayDate).getDays();
if (difference > 10) { // More than 10 days
System.out.println("You have " + difference + " Days Delay in returning your previous book");
return true;
}
return false;
}
public LocalDate getUserBorrowDate(int userID) {
String query = "SELECT Borrow_Date FROM Borrowed WHERE User_ID=?";
LocalDate date = null;
try (Connection con = DriverManager.getConnection(dbUrl, "root", "2323");
PreparedStatement ps = con.prepareStatement(query);) {
ps.setInt(1, userID);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
date = LocalDate.fromDateFields(rs.getDate("Borrow_Date"));
}
} catch (Exception e) {
e.printStackTrace();
}
return date;
}

关于java - DateTimeFormatter 中的 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857196/

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