DateTimeFormatter中的IllegalArgumentException

时间:2014-09-15 21:30:05

标签: java

这是我的例外:

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;
}

此代码之前的工作正确,但不起作用!

我的代码出了什么问题?

3 个答案:

答案 0 :(得分:1)

正如其他人所指出的那样,您从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;
}

答案 1 :(得分:0)

您的格式为"YYYY/MM/dd",但您提供"YYYY-MM-dd"

将格式更改为"YYYY-MM-dd"

答案 2 :(得分:0)

数据库以YYYY-MM-dd格式返回日期。您需要编写Java代码以与此格式保持一致。