根据圣人改变,如何求得某一年是否为闰年,并且该年的某月某日是该年哪一天

来源:互联网 发布:杭州网络诈骗拘留半年 编辑:程序博客网 时间:2024/04/29 13:25
//每四年一闰;一下是闰年的判断方法;
//1.如果能被4整除,不能被100整除,则闰
//2.但如果能被100整除,能整除400,则闰;
//3.能整除400,又能整除3200,则不闰    //3200年为平年;

//4.对于大年份的判断:能整除3200,但同时又能整除172800,则闰.


public class Test {

           static int year;    //定义年月日     
   static int month;
   static int day;
    int sum;

 public Test(int year,int month , int day){
    this.year = year;
    this.month = month;
    this.day = day;
    }

public Test() {
// TODO Auto-generated constructor stub
}


public static void main(String args[]){
    inputYMD();
    new Test().getday(month,year);
}




//输入年月日
    public static void inputYMD(){
    System.out.println("please input some day date(year mouth day):");
    Scanner in = new Scanner(System.in);
    new Test(in.nextInt(),in.nextInt(),in.nextInt());
    //判断输入是否非法
    if(year<=0 || month<=0 ||month>12 ||day<=0 ||day>31){
    System.out.println("error, please retry!");
    new Test(in.nextInt(), in.nextInt(), in.nextInt());
    }
     
    }
    
    //判断是闰年还是平年,true为闰年,false为平年
    public Boolean judgeLeapOrNoLeap(int year){
//条件1
    boolean tmp1,tmp2,tmp3,tmp4; int leap=0;  
    tmp1 = (0 == (year%4) && 0 != (year % 100));    //tmp1 = 1 说明闰
//条件2 
    tmp2 = (0 == year % 100 && 0 == year % 400);    //tmp2 = 1 说明闰
//条件3 
    tmp3 = (0 == year % 400 && 0 == year % 3200);    //tmp3 = 1 说明不闰 
//条件4 
    tmp4 = (0 == year % 3200 & 0 == year % 172800);    //tmp4 = 1 说明闰
    if(true == tmp1 || true == tmp2 || true == tmp4)
        leap = 1;        //leap = 1代表闰年
    if(true == tmp3)
        leap = 0;        //leap = 1代表平年
    if(false == tmp1 && false == tmp2 && false == tmp4)
        leap = 0;
    if(leap == 1){    //输出用户输入的年份为闰年(leap year)还是平年(non-leap year)
        System.out.println(year+"year is a leap year!");     
        return true;
    }
    else{
        System.out.println(year+"year is a non-leap year!");
        return false;
    }
    }
    
    //根据整月份的计算过总天数
    public int getday(int month,int year){
    switch(month){
    case 1: sum=0;break;
    case 2: sum=31;break;
    case 3: sum=59;break;
    case 4: sum=90;break;
    case 5: sum=120;break;
    case 6: sum=151;break;
    case 7: sum=181;break;
    case 8: sum=212;break;
    case 9: sum=243;break;
    case 10:sum=273;break;
    case 11:sum=304;break;
    case 12:sum=334;break;
    default:System.out.println("input(month) is error!");
    }
    //如果月份大于3,且为闰年,总天数需要多加1天____闰年的2月有29天。平年有28天,上述固定日期按照28天计算。    
    if (month >= 3)    
    {
        if (judgeLeapOrNoLeap(year))    //如果月份大于3,并且年份为闰年,除了月+日,再多加2月份多出来的一天。
            sum += 1 + day;
        else    
            sum += day;
    }
    else
    {
        sum += day;
    }
  //输出总天数
    System.out.println("the sum of the date is '"+ sum+ " 'days");
    return 0;
    }


}

原创粉丝点击