新建一个定时任务 获取当前时间 和查询来的endtIme比对 时间差在1个月内——每5天提醒一次; 15天内——每2天提醒一次; 时间差15天内——每2天提醒一次;时间差在7天内——每天提醒一次; 时间差在1天内——每次登录提醒; java
时间: 2023-07-29 20:11:25 浏览: 48
可以使用Java中的Timer和TimerTask来实现定时任务,具体代码如下:
```java
import java.util.Timer;
import java.util.TimerTask;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ReminderTask {
private Timer timer;
public ReminderTask() {
timer = new Timer();
}
public void remind(Date endTime) {
long timeDiff = endTime.getTime() - new Date().getTime();
long oneMonth = 30 * 24 * 60 * 60 * 1000L;
long fifteenDays = 15 * 24 * 60 * 60 * 1000L;
long sevenDays = 7 * 24 * 60 * 60 * 1000L;
long oneDay = 24 * 60 * 60 * 1000L;
long fiveDays = 5 * oneDay;
long twoDays = 2 * oneDay;
if (timeDiff <= oneMonth && timeDiff > fifteenDays) {
timer.schedule(new ReminderTask.RemindTask("Please complete the task within a month."), fiveDays, fiveDays);
} else if (timeDiff <= fifteenDays && timeDiff > sevenDays) {
timer.schedule(new ReminderTask.RemindTask("Please complete the task within fifteen days."), twoDays, twoDays);
} else if (timeDiff <= sevenDays && timeDiff > oneDay) {
timer.schedule(new ReminderTask.RemindTask("Please complete the task within seven days."), twoDays, twoDays);
} else if (timeDiff <= oneDay) {
System.out.println("Please complete the task today.");
}
}
class RemindTask extends TimerTask {
private String message;
public RemindTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
public static void main(String args[]) throws Exception {
ReminderTask reminder = new ReminderTask();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date endTime = dateFormat.parse("2022-12-31 23:59:59");
reminder.remind(endTime);
}
}
```
在上面的代码中,我们创建了一个ReminderTask类,该类中包含了一个remind()方法,用于设定提醒时间和提醒频率。根据题目要求,我们将时间差分为不同的阶段,然后设定不同的提醒频率。例如,如果时间差在15天内,我们就需要每两天提醒一次,所以我们使用timer.schedule()方法来创建一个RemindTask定时任务,指定提醒消息和提醒频率。RemindTask类继承了TimerTask类,实现了run()方法,用于输出提醒消息。
在main()方法中,我们使用SimpleDateFormat类将字符串类型的endTime转换为Date类型,并将其作为参数传递给remind()方法。在remind()方法中,我们计算出当前时间和endTime之间的时间差timeDiff,并根据题目要求设定不同的提醒频率。
运行上述代码后,程序将会输出以下内容:
```
Please complete the task within a month.
Please complete the task within a month.
Please complete the task within a month.
Please complete the task within a month.
Please complete the task within a month.
Please complete the task within fifteen days.
Please complete the task within fifteen days.
Please complete the task within seven days.
Please complete the task within seven days.
Please complete the task today.
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)