import java.time.LocalTime;
/**
* @ClassName: StringTimeExample
* @Description: 判断当前时间属于哪个时间段(2小时分隔)
* @Author: zhanghui
* @Date: 2023-12-02
* @Version: 1.0
**/
public class StringTimeExample {
public static String timeUtils(String timeData){
// 解析时间数据为 LocalTime 对象
LocalTime currentTime = LocalTime.parse(timeData);
// 定义时间段的长度(每段2小时)
int timePeriodLength = 2;
// 计算当前时间在第几个时间段
int periodIndex = currentTime.getHour() / timePeriodLength;
// 计算时间段的起始时间和结束时间
LocalTime periodStart = LocalTime.of(periodIndex * timePeriodLength, 0);
LocalTime periodEnd = periodStart.plusHours(timePeriodLength);
System.out.println("当前时间 " + timeData + " 属于时间段:" + periodStart + "-" + periodEnd);
return periodStart + "-" + periodEnd;
}
public static void main(String[] args) {
timeUtils("21:39");
}
}
判断当前时间属于哪个时间段(2小时分隔)
最新推荐文章于 2024-08-07 09:14:10 发布