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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Javacorn   2021-04-22 我要评论

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("*****************");
	}
}

代码运行结果如下:






猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们