用java實現計算日期

來源:互聯網
上載者:User

import java.util.*;

public class WeekDay

  private int month,day,countDay,toDay;
  private String engMonth=new String();
  private static boolean isTrue;
  private Scanner keyboard=new Scanner(System.in);
 
   public static void main(String args [])
   {
    
    WeekDay onedate=new WeekDay();
    //boolean bb;
    System.out.println("The program can calculate what date is the day you enter in 2008");
    do
    { onedate.getMouth();
      onedate.getDay();
      onedate.swithDate();
      onedate.calDate();
      //bb=onedate.aa();
    }while(isTrue);  //在靜態方法中無法引用非靜態成員變數,所以要把isTrue設定為static
    
    } 
    public void getMouth()
    {
       System.out.print("Enter the month(1-12)>"); 
      month=keyboard.nextInt();
      while(month>12){System.out.println("Wrong input!try again!");
                      System.out.print("Enter the month(1-12)>");
                      month=keyboard.nextInt();
                     }
    }
    public void getDay()
    {
     switch(month)
      { case 1 :
        case 3 :
        case 5 :
        case 7 :
        case 8 :
        case 10 :
        case 12 : System.out.print("Enter the day(1-31)>");
                  day=keyboard.nextInt();
                  while(day>31){System.out.println("Wrong input!try again!");
                                System.out.print("Enter the day(1-31)>");
                                day=keyboard.nextInt(); 
                               }         
                  break;
        case 2 :  System.out.print("Enter the day(1-29)>");
                  day=keyboard.nextInt();
                  while(day>29){System.out.println("Wrong input!try again!");
                                System.out.print("Enter the day(1-29)>");
                                day=keyboard.nextInt();
                               }
                  break;
        case 4 :
        case 6 :
        case 9 :
        case 11 : System.out.print("Enter the day(1-30)>");
                  day=keyboard.nextInt();
                  while(day>30){System.out.println("Wrong input!try again!");
                                System.out.print("Enter the day(1-30)>");
                                day=keyboard.nextInt();
                               }
                  System.out.println(day);            
                  break;  
                                    
       
       
       } 
    }
    public void swithDate()
    {
      switch(month)
       {
         case 1 : countDay=day; engMonth="january";break;
         case 2 : countDay=31+day; engMonth="February";break;
         case 3 : countDay=31+29+day; engMonth="March";break;
         case 4 : countDay=31*2+29+day; engMonth="Aprial";break;
         case 5 : countDay=31*2+29+30+day; engMonth="mary";break;
         case 6 : countDay=31*3+29+30+day; engMonth="June";break;
         case 7 : countDay=31*3+29+30*2+day; engMonth="July";break;
         case 8 : countDay=31*4+29+30*2+day; engMonth="August";break;
         case 9 : countDay=31*5+29+30*2+day; engMonth="September";break;
         case 10 : countDay=31*4+29+30*3+day; engMonth="October";break;
         case 11 : countDay=31*5+29+30*3+day; engMonth="Novenmber";break;
         case 12 : countDay=31*5+29+30*4+day; engMonth="December";break;
        
 
     }
       toDay=countDay%7; 
    }
    public void calDate()
    {
      switch(toDay)
       {
         
        case 0 :System.out.println(engMonth+","+day+" 2008 is Monday.");break;
        case 1 :System.out.println(engMonth+","+day+" 2008 is Teusday.");break; 
        case 2 :System.out.println(engMonth+","+day+" 2008 is Wensday.");break;
        case 3 :System.out.println(engMonth+","+day+" 2008 is Tursday.");break;
        case 4 :System.out.println(engMonth+","+day+" 2008 is Friday.");break;
        case 5 :System.out.println(engMonth+","+day+" 2008 is Saturday.");break;
        case 6 :System.out.println(engMonth+","+day+" 2008 is Sunday.");break;
       }
       System.out.print("Calculate again?(true/false)");
       isTrue=keyboard.nextBoolean(); 
    }
   /*public boolean aa()
    {
     return(isTrue); 
    }*/
    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.