查看当前日期有个参考值,参考这个日期的星期来确定算法,
19000101是星期1,如果取某天是星期2,那算法又要发生变化。
public static int count(int[] targetDate){
// 参照年月日
int[] initDate = {1900,1,1};
// 平年的月天数
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
int oldyear = initDate[0];
int oldmonth= initDate[1];
int oldday = initDate[2];
int newyear = targetDate[0];
int newmonth= targetDate[1];
int newday = targetDate[2];
int count = 0;
// 跨年为0
if(newyear-oldyear == 0){
// 跨月为0
if(newmonth-oldmonth==0){
count = newday - oldday;
}
// 跨月为1
if(newmonth-oldmonth==1){
count = newday + (days[oldmonth-1] - oldday);
}
// 跨月>1
if(newmonth-oldmonth>1){
count = (days[oldmonth-1] - oldday);
for(int i=oldmonth;i<newmonth-1;i++){
count = count + days[i];
}
count = count + newday;
}
} else {
// 当月剩余天数
count = (days[oldmonth-1] - oldday);
// 当年剩余天数
for(int i=oldmonth;i<12;i++){
count = count + days[i];
}
// 跨年超过一整年
if(newyear-oldyear>1){
count = count + 365 * (newyear-oldyear-1);
count = count + countRunNumbers(oldyear,newyear);
}
// 增加新的月份
if(newmonth>1){
for(int i=0;i<newmonth-1;i++){
count = count + days[i];
}
}
count = count + newday;
}
return count%7+1;
}
// 统计闰月的天数
public static int countRunNumbers(int oldyear,int newyear){
int count = 0;
oldyear = oldyear + 1;
for(int i=oldyear;i<=newyear;i++){
if(checkRun(i)){
count++;
}
}
return count;
}
// 检查闰年
public static boolean checkRun(int year){
boolean success = false;
if(year%400 == 0){
success = true;
} else if(year%100 == 0) {
success = false;
} else if(year%4 == 0) {
success = true;
} else {
success = false;
}
return success;
}