这是我的例外:
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;
}这段代码以前是正确的,但不工作!
我的密码怎么了?
发布于 2014-09-15 22:09:35
正如其他人所指出的,您正在从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;
}发布于 2014-09-15 21:39:53
数据库以YYYY-MM-dd格式返回日期。您需要编写与此格式一致的Java代码。
发布于 2014-09-15 21:32:00
您的格式是"YYYY/MM/dd",但您提供了一个"YYYY-MM-dd"
将格式更改为"YYYY-MM-dd"
https://stackoverflow.com/questions/25857196
复制相似问题