zoukankan      html  css  js  c++  java
  • 每天定时上传数据的实现

    自己封装一个类

    package com.anllin.utils;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class ScheduleUtil
    {
    private static DateFormat format()
    {
    return new SimpleDateFormat("HH:mm:ss:SSS");
    }

    private static Date parseTime(String time) throws ParseException
    {
    return format().parse(time);
    }

    private static long getOneDayMilliseconds()
    {
    return 24 * 60 * 60 * 1000L;
    }

    private static long getMillisecondsInOneDayBetween(String now, String future) throws ParseException
    {
    Date nowDate = parseTime(now);
    Date futureDate = parseTime(future);
    long nowTime = nowDate.getTime();
    long futureTime = futureDate.getTime();
    if (futureTime > nowTime)
    {
    return futureTime - nowTime;
    }
    else
    {
    return (futureTime + getOneDayMilliseconds()) - nowTime;
    }
    }

    public static void scheduleEveryday(ScheduledThreadPoolExecutor executor, Runnable command,
    String executedEverydayAt) throws ParseException
    {
    long initialDelay = getMillisecondsInOneDayBetween(format().format(new Date()), executedEverydayAt);
    long period = getOneDayMilliseconds();
    executor.scheduleAtFixedRate(command, initialDelay, period, TimeUnit.MILLISECONDS);
    }
    }

    调用方法

    public static void main(String[] args) throws Exception
    {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
    String executedEverydayAt = "12:00:00:000";
    ScheduleUtil.scheduleEveryday(executor, new Runnable(){
    public void run()
    {
    System.out.println("执行业务逻辑");
    }
    }, executedEverydayAt);
    }




  • 相关阅读:
    Android开发笔记——WebView
    字符串_最小表示法求循环串的最小序列(HDU_4162)
    STL_map简单应用(HDU_1075)
    DP_最大子序列和(HDU_1003)
    STL map 使用方法(转)
    数学_线性筛法建立素数表(HDU_1262)
    學習筆記 ADO數據庫訪問技術
    C#多线程学习
    Java容器
    选取单元格的基本语句
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2308054.html
Copyright © 2011-2022 走看看