1 package updateWeight; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.Timer; 6 7 import db.DbLog; 8 public class TimerManager { 9 10 public static void main(String[] args) { 11 12 DbLog.getInstance().setJarcode("updateWeight10005");// jar编码 13 new TimerManager(); 14 15 } 16 17 private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; 18 public TimerManager() { 19 Calendar calendar = Calendar.getInstance(); 20 calendar.set(Calendar.HOUR_OF_DAY, 1); //凌晨1点 21 calendar.set(Calendar.MINUTE, 1); 22 calendar.set(Calendar.SECOND, 0); 23 Date date=calendar.getTime(); //第一次执行定时任务的时间 24 //如果第一次执行定时任务的时间 小于当前的时间 25 //此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。 26 if (date.before(new Date())) { 27 date = this.addDay(date, 1); 28 } 29 Timer timer = new Timer(); 30 WeightService task = new WeightService(); 31 //安排指定的任务在指定的时间开始进行重复的固定延迟执行。 32 timer.schedule(task,date,Calendar.DAY_OF_MONTH); 33 } 34 // 增加或减少天数 35 public Date addDay(Date date, int num) { 36 Calendar startDT = Calendar.getInstance(); 37 startDT.setTime(date); 38 startDT.add(Calendar.DAY_OF_MONTH, num); 39 return startDT.getTime(); 40 } 41 public static long getPeriodDay() { 42 return PERIOD_DAY; 43 } 44 45 46 }
1 package updateWeight; 2 3 import java.util.TimerTask; 4 5 import db.ConInfo; 6 import db.DbHandle; 7 import db.DbLog; 8 9 public class WeightService extends TimerTask { 10 11 public void run() { 12 DbHandle.getInstance().setConInfo(new ConInfo( 13 "jdbc:mysql://192.168.1.180:3306/kegj?useUnicode=true&characterEncoding=UTF-8", "root", "123456")); 14 String sqlgoodssql = "update kn_goods set weight = weight - 20 where weight > 1020; "; 15 String inspirationsql = "update kn_inspiration set weight = weight - 20 where weight > 1020;"; 16 String repairmastersql = "update kn_repair_master set weight = weight - 20 where weight > 1020;"; 17 String sellerprefersql = "update kn_sellerprefer set weight = weight - 20 where weight > 1020;"; 18 19 System.out.println("-----------------------------------------"); 20 int sum1 = DbHandle.getInstance().update(sqlgoodssql); 21 int sum2 = DbHandle.getInstance().update(inspirationsql); 22 int sum3 = DbHandle.getInstance().update(repairmastersql); 23 int sum4 = DbHandle.getInstance().update(sellerprefersql); 24 System.out.println(sum1); 25 System.out.println(sum2); 26 System.out.println(sum3); 27 System.out.println(sum4); 28 System.out.println("345"); 29 DbLog.getInstance() 30 .saveLog("影响条数goods,inspiration,master,kn_sellerprefer:" + sum1 + "," + sum2 + "," + sum3 + "," + sum4); 31 32 } 33 }