www.zhblog.net

Java 定时任务中时间处理

定时任务:每天晚上2:00启动。


1.当前时间的晚上2:00为明天晚上2:00,时间天数需加1;

2.保留一个月的数据;


        try {

            long period = 1 * 24 * 60 * 60 * 1000;

            Date date = new Date();

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            long startTime = date.getTime() + period;

            String startDateText = format.format(startTime);

            startDateText = startDateText + " 02:00:00"; // 晚上2点执行

//            startDateText = "2020-08-24 10:44:00"; // 测试

            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            long start = format.parse(startDateText).getTime();

            long delay = start - date.getTime();



            ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

            future = service.scheduleAtFixedRate(()->{

                try {

                    Calendar calendar = Calendar.getInstance();

                    calendar.add(Calendar.MONTH, -1);

                    long t = calendar.getTimeInMillis();



                    Jedis jedis = JedisPoolUtil.getJedis();

                    Set<String> rks = jedis.zrangeByScore("msg:time:asc", 0, t);



                    // 同步数据库

                    for (String key : rks) {

                        LkChatHot chatHot = new Gson().fromJson(jedis.get(key), LkChatHot.class);

                        // save sql

                        String sql = "INSERT INTO lk_chathistory(ID, OWNER, WITHER, DIRECTION, MESSAGE, MESSAGETIME, BODY, MESSAGEID, RECVTYPE, MESSAGEDAY, TYPE, REVOKETYPE, REMOVETYPE) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

                        Object[] params = {chatHot.getId(), chatHot.getOwner(), chatHot.getWither(), chatHot.getDirection(), chatHot.getMessage(), chatHot.getMessageTime(), chatHot.getBody(),

                                chatHot.getMessageId(), chatHot.getRecvType(), chatHot.getMessageDay(), chatHot.getType(), chatHot.getRevokeType(), chatHot.getRemoveType()};

                        RedisToDbEntity rd = new RedisToDbEntity(sql, params, "insert", 0);

                        RedisToDbManager.add(rd);

                        // delete sql

                        String dsql = "DELETE FROM lk_chathot WHERE id=?";

                        Object[] dparams = {chatHot.getId()};

                        RedisToDbEntity drd = new RedisToDbEntity(dsql, dparams, "delete", 0);

                        RedisToDbManager.add(drd);

                    }



                    // 同步redis

                    String[] rka = new String[rks.size()];

                    rks.toArray(rka);

                    jedis.del(rka);

                    jedis.zrem("msg:time:asc", rka);

                } catch (JedisPoolUtil.JedisPoolException e) {

                    e.printStackTrace();

                } catch (Exception ex) {

                    ex.printStackTrace();

                }

            }, delay, period, TimeUnit.MILLISECONDS);

        } catch (ParseException e) {

            e.printStackTrace();

        }



 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情