From cf6c05b089adacd29ee3bfc46816548b9d5568ad Mon Sep 17 00:00:00 2001 From: lxyer <237809796@qq.com> Date: Mon, 30 Dec 2019 23:49:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=20=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/lxyer/timer/TimerTask.java | 9 +-- .../lxyer/timer/scheduled/ScheduledCycle.java | 69 +++++++++++-------- .../timer/scheduled/ScheduledExpres.java | 4 -- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/lxyer/timer/TimerTask.java b/src/main/java/com/lxyer/timer/TimerTask.java index bae1836..7062e24 100644 --- a/src/main/java/com/lxyer/timer/TimerTask.java +++ b/src/main/java/com/lxyer/timer/TimerTask.java @@ -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; } diff --git a/src/main/java/com/lxyer/timer/scheduled/ScheduledCycle.java b/src/main/java/com/lxyer/timer/scheduled/ScheduledCycle.java index a903ee1..35a94fb 100644 --- a/src/main/java/com/lxyer/timer/scheduled/ScheduledCycle.java +++ b/src/main/java/com/lxyer/timer/scheduled/ScheduledCycle.java @@ -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 diff --git a/src/main/java/com/lxyer/timer/scheduled/ScheduledExpres.java b/src/main/java/com/lxyer/timer/scheduled/ScheduledExpres.java index 3da9c0b..8721b55 100644 --- a/src/main/java/com/lxyer/timer/scheduled/ScheduledExpres.java +++ b/src/main/java/com/lxyer/timer/scheduled/ScheduledExpres.java @@ -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())); } /**