02-quartz-CronExpression
CronExpression
4类触发器,前两个是重点:
-
SimpleTriggerImpl (org.quartz.impl.triggers)
-
CronTriggerImpl (org.quartz.impl.triggers)
-
DailyTimeIntervalTriggerImpl (org.quartz.impl.triggers)
-
CalendarIntervalTriggerImpl (org.quartz.impl.triggers)
cron表达式,支持7位(springboot自带的好像不支持7位表达式,只支持6位)
星期和月份可以用英文:
占位符:
-
"*"表示所有值。秒中的"*"表示0-59每一秒
* * * * * ? *
每秒执行一次,星期要指定"?" -
"?" 只在日和星期字段允许使用。表示“不知道”,日和星期两个值必须有一个是“?”,一般把星期设置成"?"
-
"-"表示区间。
5-10 * * * * ? *
每分钟的5-10秒执行 -
"," 表示枚举
5,10 * * * * ? *
每分钟的5秒和10秒执行 -
"/"用于表示增量。
秒字段中"0/15"表示 "秒0、秒15、秒30、秒45"
秒字段中"5/15"表示 "秒5、秒20、秒35、秒50"
如果有信用卡,每笔消费的30天后,提醒还款
LocalDateTime now = LocalDateTime.now().plus(30, ChronoUnit.DAYS);
int second = now.getSecond();
int minute = now.getMinute();
int hour = now.getHour();
int day = now.getDayOfMonth();
int month = now.getMonth().getValue();
int year = now.getYear();
StringJoiner cronStr = new StringJoiner( )
.add(second+).add(minute+).add(hour+).add(day+).add(month+).add(?).add(year+);
System.out.println(cronStr.toString());
日期时间偏移
日期或时间的偏移指针对某个日期增加或减少分、小时、天等等,达到日期变更的目的。Hutool也针对其做了大量封装
String dateStr = "2017-03-01 22:33:23";
Date date = DateUtil.parse(dateStr);
//结果:2017-03-03 22:33:23
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
//常用偏移,结果:2017-03-04 22:33:23
DateTime newDate2 = DateUtil.offsetDay(date, 3);
//常用偏移,结果:2017-03-01 19:33:23
DateTime newDate3 = DateUtil.offsetHour(date, -3);
针对当前时间,提供了简化的偏移方法(例如昨天、上周、上个月等):
//昨天
DateUtil.yesterday()
//明天
DateUtil.tomorrow()
//上周
DateUtil.lastWeek()
//下周
DateUtil.nextWeek()
//上个月
DateUtil.lastMonth()
//下个月
DateUtil.nextMonth()
LocalDateTime日期偏移
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
// 增加一天
// "2020-01-24T12:23:56"
LocalDateTime offset = LocalDateTimeUtil.offset(localDateTime, 1, ChronoUnit.DAYS);
如果是减少时间,offset第二个参数传负数即可:
// "2020-01-22T12:23:56"
offset = LocalDateTimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS);