大家好,又见面了,我是你们的朋友全栈君。
package 算法;
import java.util.Calendar; import java.util.Scanner;
public class 判断年月日的那一天 {
public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); System.out.println(“输入年:”); int year=scanner.nextInt(); System.out.println(“输入月:”); int month=scanner.nextInt(); System.out.println(“输入日:”); int day=scanner.nextInt(); int sum=0; int sum2=0; //方法一 Calendar cal=Calendar.getInstance(); cal.set(year, month-1,day); sum2=cal.get(Calendar.DAY_OF_YEAR); System.out.println(“第”+sum2+” 天”); //方法2 switch (month-1) { case 12: sum+=31; case 11: sum+=30; case 10: sum+=31; case 9: sum+=30; case 8: sum+=31; case 7: sum+=31; case 6: sum+=30; case 5: sum+=31; case 4: sum+=30; case 3: sum+=31; case 2: if(year%400==0||(year%4==0||year%100==0)){ sum+=29; }else{ sum+=28; } case 1: sum+=31;
default: break; } sum+=day; System.out.println(“是第”+sum+”天”);
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106016.html原文链接:https://javaforall.cn