// 抽取了当前时间获取的逻辑,方便测试
private long currentTimeMillis;
private Date dueTime;
public Demo(Date dueTime){
this.dueTime = dueTime;
this.currentTimeMillis = getCurrentTimeMillis();
}
protected long getCurrentTimeMillis(){
return System.currentTimeMillis();
}
public long caculateDelayDays() {
if(dueTime.getTime() >= currentTimeMillis){
return 0;
}
long delayTime = currentTimeMillis - dueTime.getTime();
long delayDays = delayTime / 86400_000;
return delayDays;
}
@Test
public void testCaculateDelayDays(){
TimeZone timeZone = TimeZone.getTimeZone("Asia/ShangHai");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.clear();
calendar.set(2020, Calendar.FEBRUARY,1,0,0,0);
Date dueTime = calendar.getTime();
Demo demo = new DemoClassOne(dueTime);
Assert.assertEquals(demo.caculateDelayDays(), 0);
calendar.clear();
calendar.set(2019, Calendar.DECEMBER, 31, 0,0,0);
dueTime = calendar.getTime();
demo = new DemoClassOne(dueTime);
Assert.assertEquals(demo.caculateDelayDays(), 1);
}
public static class DemoClassOne extends Demo {
public DemoClassOne(Date dueTime) {
super(dueTime);
}
@Override
protected long getCurrentTimeMillis() {
TimeZone timeZone = TimeZone.getTimeZone("Asia/ShangHai");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.clear();
calendar.set(2020, Calendar.JANUARY,1,0,0,0);
return calendar.getTimeInMillis();
}
}
展开