java基础-查看当前日期属于星期几

查看当前日期有个参考值,参考这个日期的星期来确定算法,
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;
 }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值