首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IllegalArgumentException in DateTimeFormatter

IllegalArgumentException in DateTimeFormatter
EN

Stack Overflow用户
提问于 2014-09-15 21:30:04
回答 3查看 608关注 0票数 0

这是我的例外:

代码语言:javascript
复制
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天内尚未返回的书籍)。

代码语言:javascript
复制
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;
}

这段代码以前是正确的,但不工作!

我的密码怎么了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-15 22:09:35

正如其他人所指出的,您正在从getUserBorrowDate()获得一个格式错误的日期字符串,并最终将其传递给抛出异常的getDateDifference()。与其做所有的字符串解析,我只使用ResultSet.getDate()和比较日期。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-09-15 21:39:53

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

票数 1
EN

Stack Overflow用户

发布于 2014-09-15 21:32:00

您的格式是"YYYY/MM/dd",但您提供了一个"YYYY-MM-dd"

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25857196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档