OpenHarmony开发者论坛
标题:
OpenHarmony ArkTS 有类似于java的Time类吗,判断当前时间段是否在两个时间段之内
[打印本页]
作者:
luke
时间:
2023-12-5 13:33
标题:
OpenHarmony ArkTS 有类似于java的Time类吗,判断当前时间段是否在两个时间段之内
[md]```
类似于实现这个方法
boolean result = false;
try{
int beginHour = 00;
int beginMin = 00;
int beginSecond = 00;
int endHour = 00;
int endMin = 00;
int endSecond = 00;
beginHour = Integer.valueOf(newBeginTime.split(":")[0]);
beginMin = Integer.valueOf(newBeginTime.split(":")[1]);
beginSecond = Integer.valueOf(newBeginTime.split(":")[2]);
endHour = Integer.valueOf(newEndTime.split(":")[0]);
endMin = Integer.valueOf(newEndTime.split(":")[1]);
endSecond = Integer.valueOf(newEndTime.split(":")[2]);
final long aDayInMillis = 1000 * 60 * 60 * 24;
final long currentTimeMillis = System.currentTimeMillis();
Time now = new Time();
now.set(currentTimeMillis);
Time startTime = new Time();
startTime.set(currentTimeMillis);
startTime.hour = beginHour;
startTime.minute = beginMin;
startTime.second = beginSecond;
Time endTime = new Time();
endTime.set(currentTimeMillis);
endTime.hour = endHour;
endTime.minute = endMin;
endTime.second = endSecond;
if (!startTime.before(endTime)) {
// 跨天的特殊情况(比如22:00-8:00)
startTime.set(startTime.toMillis(true) - aDayInMillis);
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
Time startTimeInThisDay = new Time();
startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
if (!now.before(startTimeInThisDay)) {
result = true;
}
} else {
// 普通情况(比如 8:00 - 14:00)
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
}
return result;
}catch (Exception e){
e.printStackTrace();
}
// Log.e("---",">>>"+result);
return result;
```
[/md]
作者:
马迪
时间:
2023-12-5 15:16
const startTime = new Date('2023-03-17T09:00:00');
const endTime = new Date('2023-03-17T17:00:00');
const checkTime = new Date('2023-03-17T12:30:00');
if (checkTime >= startTime && checkTime <= endTime) {
console.log('The check time is within the specified time range.');
} else {
console.log('The check time is outside the specified time range.');
}
复制代码
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5