增强 时间表达式配置

This commit is contained in:
lxyer 2019-12-30 23:49:43 +08:00
parent d22238ce9e
commit cf6c05b089
3 changed files with 45 additions and 37 deletions

View File

@ -5,7 +5,8 @@ import com.lxyer.timer.task.Job;
import com.lxyer.timer.task.Task;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -39,13 +40,13 @@ public class TimerTask implements Task {
@Override
public void setScheduled(Scheduled scheduled) {
this.scheduled = scheduled;
this.theTime = scheduled.theTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
this.theTime = Date.from(scheduled.theTime().atZone(ZoneId.systemDefault()).toInstant()).getTime();
}
@Override
public long nextTime(){
LocalDateTime next = scheduled.nextTime();
this.theTime = next.toInstant(ZoneOffset.of("+8")).toEpochMilli();
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("下次执行:"+ sdf.format(next.toInstant(ZoneOffset.of("+8")).toEpochMilli()));*/
@ -54,7 +55,7 @@ public class TimerTask implements Task {
@Override
public long theTime(){
LocalDateTime next = scheduled.theTime();
this.theTime = next.toInstant(ZoneOffset.of("+8")).toEpochMilli();
this.theTime = Date.from(next.atZone(ZoneId.systemDefault()).toInstant()).getTime();
return theTime;
}

View File

@ -13,44 +13,55 @@ public class ScheduledCycle implements Scheduled {
private long period;
private TemporalUnit unit = ChronoUnit.MILLIS;
/**
* 构造方法仅限类内部使用为降低使用成本统一使用 of 的静态方法构建对象
* @param period
*/
@Deprecated
public ScheduledCycle(long period) {
this.theTime = LocalDateTime.now().plus(period, ChronoUnit.MILLIS);
this.period = period;
private ScheduledCycle() {
}
@Deprecated
public ScheduledCycle(long period,TemporalUnit unit) {
this.theTime = LocalDateTime.now().plus(period, unit);
this.period = period;
this.unit = unit;
}
@Deprecated
public ScheduledCycle(LocalDateTime startTime, long period) {
this.theTime = startTime;
this.period = period;
}
@Deprecated
public ScheduledCycle(LocalDateTime startTime, long period, TemporalUnit unit) {
this.theTime = startTime;
this.period = period;
this.unit = unit;
public static Scheduled of(String periodCfg) {
TemporalUnit unit = ChronoUnit.MILLIS;
String endchar = "";
long period;
if (periodCfg.matches("^\\d+[y,M,d,H,m,s]$")) {
endchar = periodCfg.substring(periodCfg.length() - 1);
period = Long.parseLong(periodCfg.substring(0, periodCfg.length() - 1));
} else if (periodCfg.matches("^\\d+$")) {
period = Long.parseLong(periodCfg);
if (period <= 0) {
throw new IllegalArgumentException(String.format("ScheduledCycle period config error: [%s]", periodCfg));
}
} else {
throw new IllegalArgumentException(String.format("ScheduledCycle period config error: [%s]", periodCfg));
}
if ("y".equals(endchar)) unit = ChronoUnit.YEARS;
else if ("M".equals(endchar)) unit = ChronoUnit.MONTHS;
else if ("d".equals(endchar)) unit = ChronoUnit.DAYS;
else if ("H".equals(endchar)) unit = ChronoUnit.HOURS;
else if ("m".equals(endchar)) unit = ChronoUnit.MINUTES;
else if ("s".equals(endchar)) unit = ChronoUnit.SECONDS;
return of(period, unit);
}
public static Scheduled of(long period) {
return new ScheduledCycle(period);
return of(period, ChronoUnit.MILLIS);
}
public static Scheduled of(long period,TemporalUnit unit) {
return new ScheduledCycle(period, unit);
public static Scheduled of(long period, TemporalUnit unit) {
LocalDateTime theTime = LocalDateTime.now().plus(period, unit);
return of(theTime, period, unit);
}
public static Scheduled of(LocalDateTime startTime, long period) {
return new ScheduledCycle(startTime, period);
return of(startTime, period, ChronoUnit.MILLIS);
}
public static Scheduled of(LocalDateTime startTime, long period, TemporalUnit unit) {
return new ScheduledCycle(startTime, period, unit);
ScheduledCycle scheduled = new ScheduledCycle();
scheduled.theTime = startTime;
scheduled.period = period;
scheduled.unit = unit;
return scheduled;
}
@Override

View File

@ -101,10 +101,6 @@ public class ScheduledExpres implements Scheduled{
while (theTime.isBefore(now)){
theTime = carry("m");
}
System.out.println(cfg);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("init:"+ sdf.format(this.theTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()));
}
/**