You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将字符串时间转为LocalTime,验证班次休息时长合规性

验证员工班次休息时长合规性的计算逻辑梳理

我最近在梳理一套计算逻辑,目的是验证班次员工的休息时长是否符合合规要求。调用相关API后,拿到了如下的响应数据:

{
  "StartDate": "4/25/2018",
  "EndTime": "8:30PM",
  "StartDayNumber": 1,
  "StartTime": "7:30PM",
  "SegmentTypeName": "BREAK",
  "EndDate": "4/25/2018",
  "EndDayNumber": 1
}

为了计算这段休息的时长,我尝试用Java的LocalTime类结合Duration函数来实现,目前写的代码片段如下:

LocalTime start = LocalTime.parse(employeeShift.getJSONObject(ii).get("StartTime").toString(), DateTimeFormatter.ofPattern("h:mma"));
LocalTime end = LocalTime.parse(employeeShift.getJSONObject(ii).get("EndTime").toString(), DateTimeFormatter.ofPattern("h:mma"));
Duration breakDuration = Duration.between(start, end);
// 后续可通过breakDuration判断是否符合合规时长要求

这里需要注意,因为API返回的是带AM/PM的12小时制时间,所以解析时必须指定对应的DateTimeFormatter模式(比如"h:mma"),避免出现解析错误。另外从当前数据来看,休息的开始和结束日期是同一天,直接用LocalTime计算没问题;如果遇到跨天的休息场景,可能需要结合LocalDateTime来处理,避免计算出现偏差。

内容的提问来源于stack exchange,提问作者Allen Sparks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:31:57