下面的例子是在12:30:00开始每12个小时执行一次
final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
String executeTime="12:30:00";
long periodTime=43200000;
long initialDelay=0;
try {
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dayFormat=new SimpleDateFormat("yyyy-MM-dd");
Date startTime = dateFormat.parse(dayFormat.format(new Date())+" "+executeTime);
initialDelay=startTime.getTime()-new Date().getTime();
if (initialDelay < 0) {
initialDelay = periodTime + initialDelay;
}
} catch (ParseException e) {
logger.error("Calculating the initial delay time :",e);
}
scheduledExecutor.scheduleAtFixedRate(new getTenantPbxIpsTask(), initialDelay, periodTime, TimeUnit.MILLISECONDS);