1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.redisc;
import lombok.extern.slf4j.Slf4j;
import java.time.DayOfWeek; import java.time.Duration; import java.time.LocalDateTime; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;
@Slf4j(topic = "c.Test") public class Run {
public static void main(String[] args) throws InterruptedException, ExecutionException { ScheduledExecutorService pool = Executors.newScheduledThreadPool(1); LocalDateTime now = LocalDateTime.now(); LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY); if (now.compareTo(time) > 0) { time = time.plusWeeks(1); } long initailDelay = Duration.between(now, time).toMillis(); long period = 1000 * 60 * 60 * 24 * 7; pool.scheduleWithFixedDelay(() -> { System.out.println("running..."); }, initailDelay, period, TimeUnit.MILLISECONDS); } }
|