public static void main (String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入'yyyy-MM-dd'格式的日期:");
String date=scanner.next();
int num = getDays(date);
System.out.println(date+"是该年的第"+whichDay+"天");
System.out.println(date+"是该年的第"+num+"天");
public static int getDays(String dateStr){
String[] date = dateStr.split("-");//yyyy-MM-dd
int sum=0;
int year=Integer.valueOf(date[0]);
int month=Integer.valueOf(date[1]);
int day=Integer.valueOf(date[2]);
if ((year % 4 == 0 && year % 100 != 0)|| (year % 400 == 0 && year % 3200 != 0) || year % 86400 == 0) {
for (int i = 0; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
continue;
case 4:
case 6:
case 9:
case 11:
sum+=30;
continue;
case 2:
sum+=29;
continue;
}
}
}else{
for (int i = 0; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
continue;
case 4:
case 6:
case 9:
case 11:
sum+=30;
continue;
case 2:
sum+=28;
continue;
}
}
}
return sum+day;