用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)
1、功能實(shí)現(xiàn)
輸入1查看上個(gè)月日歷輸入2查看下個(gè)月日歷輸入3查看去年本月日歷輸入4查看明年本月日歷輸入5查看指定月份日歷
2、代碼所導(dǎo)入的包
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函數(shù)和定義的屬性
static Scanner key=new Scanner(System.in);//創(chuàng)建鍵盤(pán)掃描器public static void main(String[] args) {Calendar cal=new GregorianCalendar();showTime(cal);//顯示本月日歷while(true) {help();//調(diào)出幫助菜單int num=key.nextInt();//菜單輸入選項(xiàng)switch(num) {case 1:lastMonth();break;//查找上個(gè)月日歷case 2:nextMonth();break;//查找下個(gè)月日歷case 3:lastYearMonth();break;//查找去年本月日歷case 4:nextYearMonth();break;//查找明年本月日歷case 5:chooseMonth();break;//查找指定時(shí)間日歷default :System.out.println('請(qǐng)輸入正確的指令:');}}}
4、查找去年本月日歷方法
private static void lastYearMonth() {//查找去年本月日歷Calendar cal=new GregorianCalendar();cal.add(Calendar.YEAR,-1);//將時(shí)間轉(zhuǎn)換到去年showTime(cal);//調(diào)用showTime()方法,打印日歷}
5、查找明年本月日歷
private static void nextYearMonth() {//查找明年本月日歷Calendar cal=new GregorianCalendar();cal.add(Calendar.YEAR,1);//將時(shí)間轉(zhuǎn)換到明年showTime(cal);//調(diào)用showTime()方法,打印日歷}
6、查找指定時(shí)間日歷
private static void chooseMonth() {//查找指定時(shí)間日歷System.out.println('請(qǐng)輸入時(shí)間,如 2020-2');String str=key.next();SimpleDateFormat sdf=new SimpleDateFormat('yyyy-MM');//轉(zhuǎn)換字符串時(shí)間為date類(lèi)型Date date=null;try {//拋出異常date=sdf.parse(str);} catch (ParseException e) {e.printStackTrace();}Calendar cal= new GregorianCalendar();cal.setTime(date);//將date的時(shí)間類(lèi)型轉(zhuǎn)換為CalendarshowTime(cal);////調(diào)用showTime()方法,打印日歷}
7、查找下個(gè)月日歷
private static void nextMonth() {//查找下個(gè)月日歷Calendar cal=new GregorianCalendar();cal.add(Calendar.MONTH,1);//將時(shí)間轉(zhuǎn)換到下個(gè)月showTime(cal);//調(diào)用showTime()方法,打印日歷}
8、查找上個(gè)月日歷
private static void lastMonth() {//查找上個(gè)月日歷Calendar cal=new GregorianCalendar();cal.add(Calendar.MONTH,-1);//將時(shí)間轉(zhuǎn)換到上個(gè)月showTime(cal);//調(diào)用showTime()方法,打印日歷}
9、打印幫助目錄
private static void help() {//打印幫助目錄System.out.println('*****************');System.out.println('輸入1查看上個(gè)月日歷');System.out.println('輸入2查看下個(gè)月日歷');System.out.println('輸入3查看去年本月日歷');System.out.println('輸入4查看明年本月日歷');System.out.println('輸入5查看指定月份日歷');System.out.println('*****************');}
10、該方法用來(lái)展示所搜索的時(shí)間
private static void showTime(Calendar cal) {//該方法用來(lái)展示所搜索的時(shí)間int touday=cal.getActualMaximum(Calendar.DATE);//獲取當(dāng)月的總天數(shù)cal.set(Calendar.DATE,1);//將時(shí)間設(shè)置成一個(gè)月的第一天System.out.println('一t二t三t四t五t六t日');//將星期的文字表示出來(lái)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) {//輸出換行,加上前面的空格數(shù)再換行System.out.println();}}System.out.println();System.out.println('*****************');}}
代碼運(yùn)行結(jié)果如下:
到此這篇關(guān)于用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java輸出萬(wàn)年歷內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類(lèi)別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例2. 關(guān)于HTML5的img標(biāo)簽3. php5.6不能擴(kuò)展redis.so的解決方法4. python 爬取嗶哩嗶哩up主信息和投稿視頻5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效6. python 如何停止一個(gè)死循環(huán)的線程7. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值8. JSP動(dòng)態(tài)實(shí)現(xiàn)web網(wǎng)頁(yè)登陸和注冊(cè)功能9. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送10. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總
