这是一个检查现在(当前时间)是否在 1 到 4 或 4 到 8 或 8 到 12 或 12 到 16 或 16 到 20 或 20 到 1 之间的函数,并返回下一个计算时间。
private Calendar GetTimeDiff() throws ParseException {
Calendar now = Calendar.getInstance();
Calendar one = Calendar.getInstance();
one.set(Calendar.HOUR_OF_DAY, 1);
one.set(Calendar.MINUTE, 0);
one.set(Calendar.SECOND, 0);
Calendar four = Calendar.getInstance();
four.set(Calendar.HOUR_OF_DAY, 4);
four.set(Calendar.MINUTE, 0);
four.set(Calendar.SECOND, 0);
Calendar eight = Calendar.getInstance();
eight.set(Calendar.HOUR_OF_DAY, 8);
eight.set(Calendar.MINUTE, 0);
eight.set(Calendar.SECOND, 0);
Calendar twelve = Calendar.getInstance();
twelve.set(Calendar.HOUR_OF_DAY, 12);
twelve.set(Calendar.MINUTE, 0);
twelve.set(Calendar.SECOND, 0);
Calendar sixteen = Calendar.getInstance();
sixteen.set(Calendar.HOUR_OF_DAY, 16);
sixteen.set(Calendar.MINUTE, 0);
sixteen.set(Calendar.SECOND, 0);
Calendar twenty = Calendar.getInstance();
twenty.set(Calendar.HOUR_OF_DAY, 20);
twenty.set(Calendar.MINUTE, 0);
twenty.set(Calendar.SECOND, 0);
if(now.getTime().after(one.getTime()) && now.getTime().before(four.getTime())) {
return four;
}
if(now.getTime().after(four.getTime()) && now.getTime().before(eight.getTime())) {
return eight;
}
if(now.getTime().after(eight.getTime()) && now.getTime().before(twelve.getTime())) {
return twelve;
}
if(now.getTime().after(twelve.getTime()) && now.getTime().before(sixteen.getTime())) {
return sixteen;
}
if(now.getTime().after(sixteen.getTime()) && now.getTime().before(twenty.getTime())) {
return twenty;
}
if(now.getTime().after(twenty.getTime()) && now.getTime().before(one.getTime())) {
return one;
}
return now;
}