鍵盤輸入年月日,處理輸出下一天(月底、年底、閏年) – JAVA編程語言程序開發技術文章

package test1;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Vector;

 

public class test {

public static void main(String args[])throws Exception{

Scanner sc = new Scanner(System.in);
String  date = sc.next();
        String[]  date1 = date.split("-");
        Vector<Integer> v = new Vector<>();
        for(int i=0;i<date1.length;i++){
             v.add(Integer.parseInt(date1[i]));
        }
        int year = v.get(0);
        int month = v.get(1);
        int day = v.get(2);
       
System.out.println("沒運算前"+year+" "+month+"  "+day);
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
if(month==12&&day==31){
year = year+1;
month = 1;
day = 1;
return;
}else if(day==31){
month = month+1;
day = 1;
return;
}else {
day = day+1;
}

}else{

if((year%4==0&&year%100!=0)||year%400==0){

if(month==2&&day==29){
month = month+1;
day = 1;
}else if(day==30){
month = month+1;
day = 1;
return;
}else{
day = day+1;
}

}else{
if(day==30){
month = month+1;
day = 1;
return;
}else if(month==2&&day==28){
month = month+1;
day = 1;
}else{
day = day+1;
}
}
}
System.out.println(year+"  "+month+"  "+day);
}
}

 

發佈留言