将mysql时间戳与上个月的日期进行比较

时间:2016-04-01 16:27:15

标签: java mysql

我一直在尝试从数据库的表“logging”中检索记录,其中记录的“actionTime”大于或等于上个月的日期,以便检索本月的所有日志,但是有一个问题我在Java代码中,执行查询的结果集只返回多次重复的记录。 当我在mySQL数据库控制台上执行查询时,它运行正常!但是当我在Java上使用相同的查询时,它会抛出这个问题! 这是SQL查询:

SELECT * FROM LOGGING WHERE actionTime >= '2016-03-01 00:00:00'

这是数据库表的结构: table logging

Java代码:

Date currentDate = new Date();

currentDate.setHours(0);

currentDate.setMinutes(0);

currentDate.setSeconds(0);

currentDate.setMonth(d.getMonth() - 1);

stmt = db.createStatement();

query = "SELECT * FROM LOGGING WHERE actionTime >= '2016-03-01 00:00:00'";

stmt = db.createStatement();

rs = stmt.executeQuery(query);

ArrayList<Logs> logs = new ArrayList<>();

Logs log = new Logs();

    while (rs.next()) {

        log.setID(rs.getLong("ID"));

        log.setAddress(rs.getString("address"));

        log.setDescription(rs.getString("description"));

        log.setActionDate(rs.getTimestamp("actionTime"));

        logs.add(log);

    }

1 个答案:

答案 0 :(得分:0)

尝试将log登录到while循环:

   while (rs.next()) {

    Logs log = new Logs();

    log.setID(rs.getLong("ID"));

    log.setAddress(rs.getString("address"));

    log.setDescription(rs.getString("description"));

    log.setActionDate(rs.getTimestamp("actionTime"));

    logs.add(log);

}