java定时任务与时间比较先后
最近写代码的时候,遇到这样的情况,一个页面需要用到两种不同的css样式,这个判断的标准是根据时间确定的。所以我把有关的判断放在了后台实现。
这里请重点关注一下时间比较大小的实现。
1 //判断当前时间与活动指定时间的前后关系 2 private boolean before() throws ParseException{ 3 DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4 Date myDate2 = dateFormat2.parse("2016-09-19 00:00:00"); 5 boolean before=(new Date(System.currentTimeMillis())).before(myDate2); 6 return before; 7 }
但是现在活动要延期,原来设定的19号需要更改,所以小领导让我尝试着写一个定时任务。我在网上找到了这样一段定时任务的代码:
1 public static void main(String[] args) { 2 Runnable runnable = new Runnable() { 3 public void run() { 4 // task to run goes here 5 System.out.println("Hello !!"); 6 } 7 }; 8 ScheduledExecutorService service = Executors 9 .newSingleThreadScheduledExecutor(); 10 // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间 11 service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS); 12 }
所以,我想在这块代码的基础上实现我的定时任务。

浙公网安备 33010602011771号