区分日变化并拆分第二天和java中第二天的时间

huwehgph  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(351)

我得找出一个人一天的时间长短。我需要给你一天的时间和一个人出入境的时间吗?我有一张这样的table。


# StartTime#               # EndTime#             # Day #

2018-07-16 23:00:00    2018-07-17  01:30:00      2018-07-16
2018-07-17 03:00:00    2018-07-17  04:30:00      2018-07-17
2018-07-17 13:00:00    2018-07-17  14:30:00      2018-07-17
2018-07-17 20:00:00    2018-07-17  21:30:00      2018-07-17
2018-07-17 22:00:00    2018-07-18  01:30:00      2018-07-18

我需要计算一下他一天的确切时间。例如,2018年7月17日。我需要他花的时间和日期。结果应该是

2018-07-17 00:01:00  2018-07-17  01:30:00
2018-07-17 03:00:00  2018-07-17  04:30:00
2018-07-17 13:00:00  2018-07-17  14:30:00
2018-07-17 20:00:00  2018-07-17  21:30:00   
2018-07-17 22:00:00  2018-07-17  23:59:59

我无法思考如何分割数据以及如何从表中提取数据。非常感谢您的帮助。

m4pnthwp

m4pnthwp1#

try (PreparedStatement stmt = yourDbConnection
                    .prepareStatement("select StartTime, EndTime, Day from your_table;");
            ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) {
            findTimeSpentInDay(rs.getObject("StartTime", LocalDateTime.class),
                    rs.getObject("EndTime", LocalDateTime.class),
                    rs.getObject("Day", LocalDate.class));
        }
    }

上面的代码段使用以下方法查找时间:

private static void findTimeSpentInDay(
        LocalDateTime startTime, LocalDateTime endTime, LocalDate day) {
    LocalDateTime startOfDay = day.atStartOfDay();
    LocalDateTime endOfDay = day.plusDays(1).atStartOfDay();
    if (startTime.isBefore(startOfDay)) {
        startTime = startOfDay;
    }
    if (endTime.isAfter(endOfDay)) {
        endTime = endOfDay;
    }
    System.out.println("" + startTime + "  " + endTime);
}

输出不完全是你说的,主要是因为我不明白你的确切要求:

2018-07-16T23:00  2018-07-17T00:00
2018-07-17T03:00  2018-07-17T04:30
2018-07-17T13:00  2018-07-17T14:30
2018-07-17T20:00  2018-07-17T21:30
2018-07-18T00:00  2018-07-18T01:30

我认为它接近了,我将留给您调整代码以达到所需的效果。
免责声明:从mysql获取的代码已经编译,但是我还没有运行它(手头没有mysql安装)。我运行了辅助方法,它产生了我引用的输出。
你不配得到这个答案,因为你的问题太宽泛,不清楚,研究也不充分。我多走了一公里给你至少一点答案。下一次请多走一公里问一个好问题,这样好的答案就可以写出来,而不需要猜测和太多的压力。这将是所有人的荣幸,尤其是你自己。

相关问题