gpt4 book ai didi

java - 如何在Java中返回LocalTime的总和

转载 作者:行者123 更新时间:2023-12-02 08:42:21 25 4
gpt4 key购买 nike

它不会得到 session 记录。我需要返回分钟数。如何在 Java 中迭代 Localtime 时返回分钟总和?

public String userLunchHoursSum(String username) {
List<WorkHour> workHours = workHourRepository.findWorkHoursByUsername(username);
System.out.println(Arrays.toString(workHours.toArray()));
long diff = 0;
LocalTime lunchTime;
long minutes = 0;
LocalTime plusMinutes = null;
for (WorkHour workHour : workHours) {
lunchTime = workHour.getLunch_time().toLocalTime(); //00:30:00
plusMinutes = lunchTime.plusMinutes(lunchTime.getMinute());
}
if(workHours.size()!= 0) {
return Long.toString(plusMinutes.getMinute());
}
return "00:00";
}

getLunch_time 返回java.sql.Time

最佳答案

如上所述,您应该存储持续时间而不是本地时间。如果这是您无法控制的,请考虑迁移数据库或创建中间解析函数。我还没有运行示例代码,因为我不知道 WorkHour 中有什么。

// leave the string formatting to other functions
public long userLunchHoursSum(String username) {
List<WorkHour> workHours = workHourRepository.findWorkHoursByUsername(username);
Duration totalDuration = Duration.ZERO;
for (WorkHour workHour : workHours) {
// save your time in the appropriate format beforehand
// do not use local time to store duration.
Duration lunchTime = Duration.between(LocalTime.MIDNIGHT, workHour.getLunch_time().toLocalTime()); //00:30:00
totalDuration = totalDuration.plus(lunchTime);
}
return totalDuration.toMinutes();
}

关于java - 如何在Java中返回LocalTime的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61304875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com