程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 輸出第二天的日期

輸出第二天的日期

編輯:關於JAVA


package AddOneDate;

import java.util.*;
import javax.swing.*;
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        boolean flag=true;
        int year=0,month=0,day=0;
        StringTokenizer pas;
        String date[]=new String[3];
        String message[]= {"","",""};
        while(flag)
        {
            flag=false;
            String inputValue = JOptionPane.showInputDialog("請輸入年月日,輸入格式為:****/**/**");
            pas = new StringTokenizer(inputValue,"//");
             int i=0;
            while(pas.hasMoreTokens())
                date[i++]=pas.nextToken();
            year=Integer.parseInt(date[0]);
            month=Integer.parseInt(date[1]);
            day=Integer.parseInt(date[2]);
            if(year<=0) message[0]="你輸入的年份不合法!";
            if(month<=0||month>12) message[1]="你輸入的月份不合法!";
                switch(month)
                {
                     case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                     {
                        if(day<=0||day>31)
                            message[2]="你輸入的日期不合法!";
                         break;
                    }
                     case 4:case 6:case 9:case 11:
                    {
                       if(day<=0||day>30)
                          message[2]="你輸入的日期不合法!";
                       break;
                    }
                    case 2:
                    {
                        if(year%4==0&&year%100!=0||year%400==0)
                         {
                            if(day<=0||day>=30)
                                message[2]="你輸入的日期不合法!";
                            break;
                         }
                         else
                          {
                             if(day<=0||day>=29)
                                 message[2]="你輸入的日期不合法!";
                             break;
                         }
                     }                 
                }
            if(message[0].length()!=0||message[1].length()!=0||message[2].length()!=0)
            {
                flag=true;
                for(int j=0;j<3;j++)
                    message[j]="";
                JOptionPane.showMessageDialog(
                  null,
                  "你輸入的日期不合法!",
                  "警告信息",
                  JOptionPane.DEFAULT_OPTION );
            }
        }

        System.out.println("你輸入的日期是:"+date[0]+"年"+date[1]+"月"+date[2]+"日");
       
       
        switch(month)
        {
            case 1:case 3:case 5:case 7:case 8:case 10:
            {
                if(day<=30)
                {day++;}
                else
                {
                    month++;day=1;
                }
                  break;
            }

            case 4:case 6:case 9:case 11:
            {
                if(day<=29)
                {day++;}
                else
                {day=1;month++;}
                break;
            }

            case 2:
            {
                if(year%4==0&&year%100!=0||year%400==0)
                {
                    if(day<=28){day++;}
                    else
                    {day=1;month++;}
                }
                else
                {
                    if(day<=27){day++;}
                    else
                    {day=1;month++;}
                }
                break;
            }
            case 12:
            {
                if(day<=30){day++;}
                else
                {day=1;month=1;year++;}
                break;
            }
        }
        System.out.println("第二天的日期為:"+year+"年"+month+"月"+day+"日");
    }
}

*
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved