OpenHarmony开发者论坛
标题:
OpenHarmony ArkTS 是否有类似于java的Time类,可以通过Time.before及after方法判断两个时间的先后
[打印本页]
作者:
luke
时间:
2023-12-5 13:48
标题:
OpenHarmony ArkTS 是否有类似于java的Time类,可以通过Time.before及after方法判断两个时间的先后
[md]类似于下面的代码
```
public static boolean isCurrentInTimeScope(String newBeginTime, String newEndTime) {
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:13
<div>const date1 = new Date('2023-03-17');
const date2 = new Date('2023-03-18');
if (date1 > date2) {
console.log('date1 is later than date2');
} else if (date1 < date2) {
console.log('date1 is earlier than date2');
} else {
console.log('date1 is the same as date2');
}</div>
复制代码
作者:
hyacinth养花人
时间:
2023-12-6 10:42
标题:
OpenHarmony ArkTS 是否有类似于java的Time类,可以通过Time.before及after方法判断两个时间的先后
三方库
dayjs
(, 下载次数: 13)
上传
点击文件名下载附件
作者:
Bragg
时间:
2024-5-10 10:18
回复
hyacinth养花人
: 这个库做的不是标准的格式化,没法使用,dd居然不是表示day, 而是表示星期?,不支持yyyy只支持YYYY
作者:
hyacinth养花人
时间:
2024-5-11 09:00
回复
Bragg
: 这样啊,可以提个ISSURE
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5