public void schedule(User user) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, user.getHour()); calendar.set(Calendar.MINUTE, user.getMinute()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); timer.scheduleAtFixedRate(new ReminderTask(user), calendar.getTime(), user.getFrequency()); }
时间: 2023-11-19 13:41:26 浏览: 89
这段代码是一个调度函数,它接受一个User对象作为参数。函数中首先创建一个Calendar对象来获取当前时间。然后使用User对象的getHour()和getMinute()方法设置Calendar对象的小时和分钟。将秒和毫秒设置为0。接着使用timer.scheduleAtFixedRate()方法来定时执行一个ReminderTask任务。这个任务的执行时间是calendar.getTime(),即设置的时间,执行频率为user.getFrequency(),即用户设置的频率。这段代码的作用是根据用户设置的时间和频率来定时执行任务。
相关问题
public static int getAge(Date birth) { Calendar cal = Calendar.getInstance(); int thisYear = cal.get(Calendar.YEAR); int thisMonth = cal.get(Calendar.MONTH); int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); cal.setTime(birth); int birthYear = cal.get(Calendar.YEAR); int birthMonth = cal.get(Calendar.MONTH); int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); int age = thisYear - birthYear; // 未足月 if (thisMonth <= birthMonth) { // 当月 if (thisMonth == birthMonth) { // 未足日 if (dayOfMonth < birthdayOfMonth) { age--; } } else { age--; } } return age; }
下面是代码的简化版本:
```
public static int getAge(Date birth) {
Calendar cal = Calendar.getInstance();
cal.setTime(birth);
int birthYear = cal.get(Calendar.YEAR);
int birthMonth = cal.get(Calendar.MONTH);
int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
int thisMonth = Calendar.getInstance().get(Calendar.MONTH);
int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int age = thisYear - birthYear;
if (thisMonth <= birthMonth && thisMonth == birthMonth && dayOfMonth < birthdayOfMonth) {
age--;
} else {
age--;
}
return age;
}
```
这个简化版本将一些变量的声明和初始化放在了一起,同时将一些重复的代码进行了简化。
public class TodayUtil { public static String getToday(){ Calendar calendar = Calendar.getInstance(); String year = String.valueOf(calendar.get(Calendar.YEAR)); String month = String.valueOf(calendar.get(Calendar.MONTH)+1); String date = String.valueOf(calendar.get(Calendar.DATE)); String day = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)); String minute = String.valueOf(calendar.get(Calendar.MINUTE)); String second = String.valueOf(calendar.get(Calendar.SECOND)); if(calendar.get(Calendar.MONTH)+1 < 10) month = "0"+month; if(calendar.get(Calendar.DATE) < 10) date = "0"+date; return year+"-"+month+"-"+date+" "+day+":"+minute+":"+second; } public static String getTodayYmd(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String result = format.format(today); return result; } //获取过去的第past天 public static String getPastDate(int past) { Calendar calendar = Calendar.getInstance(); //System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past); Date today = calendar.getTime(); //System.out.println(today); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String result = format.format(today); return result; } }
这是一个Java工具类,提供了以下方法:
1. getToday():获取当前日期和时间,格式为 yyyy-MM-dd HH:mm:ss。
2. getTodayYmd():获取当前日期,格式为 yyyy-MM-dd。
3. getPastDate(int past):获取过去第 past 天的日期,格式为 yyyy-MM-dd。
这些方法都是基于当前系统时间的 Calendar 对象进行计算得出的。
阅读全文