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

//输出首日前面的空格

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

}

}

代码运行结果如下:

ff9bd8844bf7c0a325b16333547ee09a.png

2c7d2f17886b20ac599d884a045d3c09.png

7703470b957239e3be55673aa01ff8f9.png

8deb19d4ab7d0c6390c57f26e7bee72d.png

6e30d79357f6c9c96eeac87a51c1d048.png

6e07793c43cece560e3145b99f94ae8c.png

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐