栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用Java编程输出万年历的功能实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用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

代码运行结果如下:






到此这篇关于用Java编程输出万年历的功能实现的文章就介绍到这了,更多相关Java输出万年历内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号