*/
@Configuration
@EnableScheduling
public class TaskManger {
@Autowired
ZyCouponRecordServiceImpl zyCouponRecorService;
/*
* 每天0点0分0秒运行 更改 失效状态
* */
@Scheduled(cron = "0 0 0 * * ?")
public void billTask( ) {
List<ZyCouponRecord> zyCouponRecords =new ArrayList<>();
List<ZyCouponRecord> zyCouponRecordlist = zyCouponRecorService.lambdaQuery().eq(ZyCouponRecord::getRecordState, 0).list();
System.out.println(zyCouponRecordlist.size()+"zyCouponRecordlist");
for (int a=0;a<zyCouponRecordlist.size();a++){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatDate = sdf.format(zyCouponRecordlist.get(a).getCtime());
if( DateUtil.getdatas(zyCouponRecordlist.get(a).getRecordCouponTerm()).equals(formatDate) ){
zyCouponRecordlist.get(a).setRecordState(2);
zyCouponRecords.add(zyCouponRecordlist.get(a));
}
}
zyCouponRecorService.updateBatchById(zyCouponRecords);
}
/*
* 每天0点30分0秒运行 更改 失效状态
* */
@Scheduled(cron = "0 30 0 * * ?")
public void Task( ) {
zyCouponRecorService.lambdaUpdate()
.ne(ZyCouponRecord::getRecordState,0)
.eq(ZyCouponRecord::getRecordDisplay, 0)
.eq(ZyCouponRecord::getEtime,DateUtil.getSevenAgo())
.set(ZyCouponRecord::getRecordDisplay, 1).update();
}
}
时间转换工具
import org.apache.commons.lang3.time.DateUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 时间获取工具类(用于首页数据统计使用)
*/
public class DateUtil {
public static void main(String[] args) {
System.out.println(getSixMonth());
}
public static String getToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format = sdf.format(new Date());
return format;
}
public static String getBillId() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String format = sdf.format(new Date());
return format;
}
public static String getSevenAgo() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = DateUtils.addDays(new Date(), -2);
String formatDate = sdf.format(date);
return formatDate;
}
public static boolean getDayBetweenTwoDate(String startDateStr, String endDateStr) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
boolean b = false;
try {
Date startDate = simpleDateFormat.parse(startDateStr);
Date endDate = simpleDateFormat.parse(endDateStr);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
//计算两个日期相差的天数
//startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
//1天=24小时,1小时=60分,1分=60秒,所以两个时间的差再除以60 * 60 * 24换算成天的形式
int days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60 * 24);
if (days < 7) {
b = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return b;
}
//获取近七天日期
public static List<String> getSevenDate() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 7; i++) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
//获取近一个月日期
public static List<String> getOneMonth() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 30; i++) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
//获取近半年内月份
public static List<String> getSixMonth() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
for (int i = 0; i < 6; i++) {
Date date = DateUtils.addMonths(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
public static String getLastMonth() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date = DateUtils.addMonths(new Date(), -1);
String formatDate = sdf.format(date);
return formatDate;
}
public static String getdatas(int a) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = DateUtils.addDays(new Date(), -a);
String formatDate = sdf.format(date);
return formatDate;
}
}