gpt4 book ai didi

java - 如何获取当前时间和日期并设置在我的应用程序中执行操作的特定时间

转载 作者:行者123 更新时间:2023-12-01 19:52:42 26 4
gpt4 key购买 nike

我的应用程序每天在午夜0000点获得积分,所以我想设置这个时间,当时间到达时,我希望我的应用程序自动添加积分,即使应用程序没有打开。

我尝试了下面的代码,但它不起作用

//creates a time for 0000am
String newDayTime = "00:00:00";
Date date = java.sql.Time.valueOf(newDayTime);
Calendar newDayDate = Calendar.getInstance();
newDayDate.setTime(date);
if(((today.getTime().after(newDayDate.getTime())))){
dataa.edit().putFloat("blToday", blToday(select.this) + 15f).apply();
}

这段代码的问题是,当凌晨 12:00 到达时它不会添加积分,并且每次启动应用程序时它都会添加积分,每次打开应用程序时它都会添加积分,我该如何更正我的代码?

最佳答案

我认为你应该使用ScheduledExecutorService。它将在计划的时间执行提交的任务(Runnable/Callable),并且该任务可以定期重复。<​​/p>

创建如下任务:

class UpdatePointWorker implements Runnable {
@Override
public void run() {
// write your code here to update the ponints
// dataa.edit().putFloat("blToday", blToday(select.this) + 15f).apply();
}
}

按如下方式安排任务:

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Long midnight = LocalDateTime.now().until(LocalDate.now().plusDays(1).atStartOfDay(), ChronoUnit.MINUTES);
Runnable task = new UpdatePointWorker();
scheduler.scheduleAtFixedRate(task, midnight, 1440, TimeUnit.MINUTES);

关于java - 如何获取当前时间和日期并设置在我的应用程序中执行操作的特定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com