spring boot 定时 只保留半个月日志

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 19:35   57   0
package com.rate.system.rate_system.timeTask;

import com.rate.system.rate_system.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author gaojie
 * @Date 2019/8/28 10:49
 * @Version 1.0
 */
@Component
@Configuration
@EnableScheduling  //开启定时任务
public class Log {

    @Autowired
    LogService logService;
    /**
     * 定时删除半个月前的数据
     * @author gaojie
     * @date 2019/8/28 11:00
     * @param
     * @return void
     **/
    // @Scheduled(cron = "0 */1 * * * ?") //每分钟
    @Scheduled(cron = "0 0 1 * * ?")  //每天一点
    public void delLog() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c = Calendar.getInstance(); //
        c.setTime(new Date());
        c.add(Calendar.DATE, - 15);
        Date d = c.getTime();
        String date = format.format(d);
        //System.out.println("过去15天:"+date);

        logService.delectByGmtcreatetime(date);
    }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP