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

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

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输出万年历内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多