歡迎您光臨本站 註冊首頁

用Java編程輸出萬年曆的功能實現

←手機掃碼閱讀     limiyoyo @ 2020-06-05 , reply:0

1、功能實現
 

輸入1查看上個月日曆
 輸入2查看下個月日曆
 輸入3查看去年本月日曆
 輸入4查看明年本月日曆
 輸入5查看指定月份日曆
 

2、代碼所導入的包

  import java.text.ParseException;  import java.text.SimpleDateFormat;  import java.util.Calendar;  import java.util.Date;  import java.util.GregorianCalendar;  import java.util.Scanner;

 

3、main函數和定義的屬性

  static Scanner key=new Scanner(System.in);//創建鍵盤掃描器  	public static void main(String[] args) {  		Calendar cal=new GregorianCalendar();  		showTime(cal);//顯示本月日曆  		while(true) {  		help();//調出幫助菜單  			int num=key.nextInt();//菜單輸入選項  			switch(num) {  			case 1:lastMonth();break;//查找上個月日曆  			case 2:nextMonth();break;//查找下個月日曆  			case 3:lastYearMonth();break;//查找去年本月日曆  			case 4:nextYearMonth();break;//查找明年本月日曆  			case 5:chooseMonth();break;//查找指定時間日曆  			default :System.out.println("請輸入正確的指令:");  			}  		}    	}

 

4、查找去年本月日曆方法

  private static void lastYearMonth() {//查找去年本月日曆  		Calendar cal=new GregorianCalendar();  		cal.add(Calendar.YEAR,-1);//將時間轉換到去年  		showTime(cal);//調用showTime()方法,打印日曆  		  	}

 

5、查找明年本月日曆

  private static void nextYearMonth() {//查找明年本月日曆  		Calendar cal=new GregorianCalendar();  		cal.add(Calendar.YEAR,1);//將時間轉換到明年  		showTime(cal);//調用showTime()方法,打印日曆  		  	}

 

6、查找指定時間日曆

  private static void chooseMonth() {//查找指定時間日曆  		System.out.println("請輸入時間,如 2020-2");  		String str=key.next();  		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");  		//轉換字符串時間為date類型  		Date date=null;  		try {//拋出異常  			date=sdf.parse(str);  		} catch (ParseException e) {  			e.printStackTrace();  		}  		Calendar cal= new GregorianCalendar();  		cal.setTime(date);//將date的時間類型轉換為Calendar  		showTime(cal);////調用showTime()方法,打印日曆  	}

 

7、查找下個月日曆

  private static void nextMonth() {//查找下個月日曆  		Calendar cal=new GregorianCalendar();  		cal.add(Calendar.MONTH,1);//將時間轉換到下個月  		showTime(cal);//調用showTime()方法,打印日曆  		  	}

 

8、查找上個月日曆

  private static void lastMonth() {//查找上個月日曆  		Calendar cal=new GregorianCalendar();  		cal.add(Calendar.MONTH,-1);//將時間轉換到上個月  		showTime(cal);//調用showTime()方法,打印日曆  		  	}

 

9、打印幫助目錄

  private static void help() {//打印幫助目錄  		System.out.println("*****************");  		System.out.println("輸入1查看上個月日曆");  		System.out.println("輸入2查看下個月日曆");  		System.out.println("輸入3查看去年本月日曆");  		System.out.println("輸入4查看明年本月日曆");  		System.out.println("輸入5查看指定月份日曆");  		System.out.println("*****************");  	}

 

10、該方法用來展示所搜索的時間

  private static void showTime(Calendar cal) {//該方法用來展示所搜索的時間  		int touday=cal.getActualMaximum(Calendar.DATE);  		//獲取當月的總天數  		cal.set(Calendar.DATE,1);  		//將時間設置成一個月的第一天  		System.out.println("一	二	三	四	五	六	日");  		//將星期的文字表示出來  		int weekday=cal.get(Calendar.DAY_OF_WEEK);  		//獲取每月第一天是星期幾  		for(int i=1;i<weekday-1;i++) {  			//輸出首日前面的空格  			System.out.print("	");  			}  		for(int i=1;i<=touday;i++) {  			//將一月裡的每一天輸出  			System.out.print(i+"	");  			if((i+weekday-2)%7==0) {  				//輸出換行,加上前面的空格數再換行  				System.out.println();  			}  		}  		System.out.println();  		System.out.println("*****************");  	}  }

代碼運行結果如下:
 


 


 


 


 


 

                        

   


[limiyoyo ] 用Java編程輸出萬年曆的功能實現已經有244次圍觀

http://coctec.com/docs/java/show-post-237062.html