1. 程式人生 > 程式設計 >用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

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("一\t二\t三\t四\t五\t六\t日");
		//將星期的文字表示出來
		int weekday=cal.get(Calendar.DAY_OF_WEEK);
		//獲取每月第一天是星期幾
		for(int i=1;i<weekday-1;i++) {
			//輸出首日前面的空格
			System.out.print("\t");
			}
		for(int i=1;i<=touday;i++) {
			//將一月裡的每一天輸出
			System.out.print(i+"\t");
			if((i+weekday-2)%7==0) {
				//輸出換行,加上前面的空格數再換行
				System.out.println();
			}
		}
		System.out.println();
		System.out.println("*****************");
	}
}

程式碼執行結果如下:

用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

用Java程式設計輸出萬年曆的功能實現

到此這篇關於用Java程式設計輸出萬年曆的功能實現的文章就介紹到這了,更多相關Java輸出萬年曆內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!