久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

瀏覽:30日期:2022-08-31 18:28:31

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é)果如下:

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

到此這篇關(guān)于用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java輸出萬(wàn)年歷內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 安仁县| 公主岭市| 晋江市| 唐河县| 沙河市| 九龙坡区| 奈曼旗| 盘山县| 磐石市| 商城县| 宁晋县| 隆化县| 萨迦县| 开阳县| 洛浦县| 汶川县| 青阳县| 定州市| 莲花县| 昌都县| 涿鹿县| 资源县| 莲花县| 定西市| 杭锦旗| 天门市| 萝北县| 江北区| 冕宁县| 兰州市| 永修县| 明溪县| 宜昌市| 翼城县| 久治县| 额尔古纳市| 平湖市| 玉树县| 龙岩市| 黄冈市| 衡南县|