计算某个日子是该年的第几天
使用switch语句怎么写
使用switch语句怎么写
2018-09-26
public static void main(String[] args) { Scanner input = new Scanner(System.in); Exercises exe = new Exercises(); System.out.print("请输入要计算的日期:"); // 2018-09-26 int day = exe.getDay(input.next()); System.out.println(day); } /** * 获取输入值所对应该年的第几天 * * @param date * @return */ public int getDay(String date) { String[] ymd = date.split("-"); int inputYear = Integer.valueOf(ymd[0]); int inputMonth = Integer.valueOf(ymd[1]); int inputDay = Integer.valueOf(ymd[2]); int day = 0; // 天数 Exercises exe = new Exercises(); for (int i = 1; i <= (inputMonth - 1); i++) { day = day + exe.getMonthLastDay(i, inputYear); } return day + inputDay; } /** * 获取每个月的天数 * * @param month * @param year * @return */ public int getMonthLastDay(int month, int year) { int day = 31; switch (month) { case 4: case 6: case 9: case 11: day = 30; break; case 2: if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { // 判断是否是闰年 day = 29; } else { day = 28; } break; } return day; }
举报