java 判断某一天是当年的哪一天

时间:2022-08-23 07:05:06

题目:输入年份,月份,日,判断这一天是这一年的第几天?(闰年的2月份为29天,平年为28天)

public class Runnian {

    /**
* 能被4整除且不能被100整除或者能被400整除的年份为闰年
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入年,月,日");
Scanner sc1=new Scanner(System.in);
int year=sc1.nextInt();
Scanner sc2=new Scanner(System.in);
int month=sc2.nextInt();
Scanner sc3=new Scanner(System.in);
int day=sc3.nextInt();
int totalDay=0;
for(int i=1;i<month;i++){
totalDay+=getMonthDay(year,i);
System.out.println("总天数为"+totalDay);
}
System.out.println("总天数为"+(totalDay+day)); }
public static int getMonthDay(int year,int month){
boolean flag=isRunnian(year);
if(month==2){
if(flag==true){
return 29;
}else{
return 28;
}
}else if(month==4||month==6||month==9||month==11){
return 30;
}else{
return 31;
}
}
public static boolean isRunnian(int year){
if((year%4==0&&year%100!=0)||(year%400==0)){
return true;
}else{
return false;
} }
}