定时任务-java

来源:互联网 发布:阿里云os 编辑:程序博客网 时间:2024/06/14 07:43
package com.offcial.service.Impl;


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


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.offcial.entity.Customer;
import com.offcial.service.CustomerService;


/*
 * 定时器
 */
@Component
public class TaskCool {
/**
     * 第一个定时器测试方法
     */
@Autowired
private CustomerService customerService;

    public void testJob() throws ParseException{
    //查出所有的
    List<Customer> list= customerService.customerQuery();
    for(Customer cus :list ){
    String phoneType=cus.getPhoneType();
    String warrantyType=cus.getWarrantyType();
    Date billDate=cus.getBillDate();
    Date date=new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(billDate);
    //判断是否失效,单据日期+延保类型
    int year= c.get(Calendar.YEAR);    //获取完整的年份(4位,1970-????)
    int month= c.get(Calendar.MONTH)+1;       //获取当前月份(0-11,0代表1月)
    int day= c.get(Calendar.DAY_OF_MONTH);
    if(warrantyType.equals("1")){
    //延长保  +2
    year=year+2;
    }else if(warrantyType.equals("2") || warrantyType.equals("4")){
    //整机一体保 ,意外保12个月 +1
    year=year+1;
    }else if(warrantyType.equals("3")){
    //意外保6个月  +6
    month=month+6;
    if(month-12>0){
    year=year+1;
    month=month-12;
    }
    year=year+1;
    }
    String result =String.valueOf(year) +'-'+ String.valueOf(month)+'-'+ String.valueOf(day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date expire = format.parse(result);
    if(expire.getTime()<date.getTime()){
    //失效日期小于当前日期,已失效状态为3
    if(cus.getState()==0 || cus.getState()==1 || cus.getState()==2 || cus.getState()==4 ){
    cus.setState(3);
    cus.setCreatorNumber("");
    customerService.changeUpdate(cus);
    }
    }
    }
    }

}



spring-servlet.xml
 <task:scheduled-tasks>
        <task:scheduled ref="taskCool" method="testJob" cron="0 0 0 * * ?"/>
    </task:scheduled-tasks>


原创粉丝点击