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

网友投稿 369 2023-05-31

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 lastYearMhttp://onth() {//查找去年本月日历

Calendar cal=new GregorianCalendar();

cal.add(Calendar.YEAR,-1);//将时间转换到去年

showTime(cal);//调用showTime()方法,打印日历

}

5、查找明年本月日历

private static void nextYearMonth() {//查找明年本月日历

Chttp://alendar 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 http://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("*****************");

}

}

代码运行结果如下:

//输出首日前面的空格

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

}

}

代码运行结果如下:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot中@Async默认线程池导致OOM问题
下一篇:详解IntelliJ IDEA2020.1和JDK14体验
相关文章

 发表评论

暂时没有评论,来抢沙发吧~