用java实现计算日期

来源:互联网 发布:88xoxo的域名换成什么 编辑:程序博客网 时间:2024/06/05 23:55

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); 
    }*/