版权声明:以上所有博客均为冷漠的小猿所有,引用请注明出处 https://blog.csdn.net/lyf1997115/article/details/81569028
日历小程序
功能
- 查看当前日期所在月份的日历
- 输入年份和月份,显示指定年月的日历
- 实现日历的翻页功能
界面类
public class UI {
public void u_1() {
System.out.println("\t 日历小程序\n");
System.out.println("*****************************");
System.out.println("*****************************");
System.out.println("**** ****");
System.out.println("**** ****");
System.out.println("**** 1.显示当前月 ****");
System.out.println("**** ****");
System.out.println("**** 2.查找指定月 ****");
System.out.println("**** ****");
System.out.println("**** 0.退出 ****");
System.out.println("**** ****");
System.out.println("**** ****");
System.out.println("*****************************");
System.out.println("*****************************");
System.out.println("*****************************");
}
}
日历类
负责接收数据并生成日历
import java.util.Calendar;
import com.lyf.date.MyDateOperation;
/**
* 这是一个日历类
* 1.可以打印当前月的日历
* 2.可以根据输入参数,打印特定月份日历
*
* @author lyf
* @serial 1.0
* @since 1.0
*/
public class MyCalendar {
private int year;
private int month;
private Calendar cal;
/**
* 无参构造,默认当前月
*/
public MyCalendar() {
cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
this.setYear(this.cal.get(Calendar.YEAR));
this.setMonth(this.cal.get(Calendar.MONTH) + 1);
}
/**
* 有参构造,根据输入的年份和月份设置为对应月
* @param year 查找年份
* @param month 查找月份
*/
public MyCalendar(int year, int month) {
this.setYear(year + month / 12);
this.setMonth(month);
cal = Calendar.getInstance();
cal.set(this.year, this.month - 1, 1);
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month % 12;
}
/**
* 打印日历表
*/
public void table() {
MyDateOperation mdo = new MyDateOperation();
int maxDay = mdo.getMonthDays(this.cal.get(Calendar.YEAR), this.cal.get(Calendar.MONTH));
int begin = (cal.get(Calendar.DAY_OF_WEEK) + 6) % 7;
int max = begin + maxDay;
System.out.println("================== " + String.valueOf(this.cal.get(Calendar.YEAR)) + "年-"
+ String.valueOf(this.cal.get(Calendar.MONTH) + 1) + "月 =================");
System.out.println("\n日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < max; i++) {
if (i % 7 == 0) {
System.out.println();
}
if (i >= begin) {
System.out.print((i - begin + 1) + "\t");
} else {
System.out.print("\t");
}
}
System.out.println();
}
}
日期时间操作类
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 这是一个日期时间操作类
*
* @author lyf
* @serial 1.0
*
*/
public class MyDateOperation {
/**
* 返回日期相隔天数
* @param beginDate 开始日期
* @param endDate 结束日期
* @return int 相隔天数
*/
public static int getTimeDistance(Date beginDate, Date endDate) {
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
long beginTime = beginCalendar.getTime().getTime();
long endTime = endCalendar.getTime().getTime();
int betweenDays = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));
if (betweenDays < 0) {
System.out.println("穿越了吧,兄弟!");
} else if (betweenDays == 0
&& beginCalendar.get(Calendar.DAY_OF_MONTH) != endCalendar.get(Calendar.DAY_OF_MONTH)) {
return betweenDays + 1;
}
return betweenDays;
}
/**
* 格式化输出当前日期
* @param date Date型日期参数
* @return String 格式化日期
*/
public static String formatPrint(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
return sdf.format(date) ;
}
/**
* 获得指定月份的总天数
* @param year
* @param month
* @return int 总天数
*/
public static int getMonthDays(int year, int month)
{
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month);
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
a.add(Calendar.MARCH, -1);//日期回滚一天,也就是最后一天
return maxDate;
}
}
主程序类
实现各项功能,负责整个程序有序的执行
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 日历小程序 具备功能: 1.查询当前月日历 2.查询指定月日历 3.日历翻页
*
* @author lyf
*
*/
public class CalendarProgram {
Scanner input = new Scanner(System.in);
private MyCalendar calendar = null ;
private int year ;
private int month ;
/**
* 无参构造,默认当前月
*/
public CalendarProgram() {
calendar = new MyCalendar() ;
this.setYear(calendar.getYear());
this.setMonth(calendar.getMonth());
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
/**
* 查看指定月份日历
*/
public void search() {
System.out.print("请输入要查询年份: ");
this.setYear(inputInt());
System.out.print("请输入要查询月份: ");
this.setMonth(inputInt());
getTable(this.getYear(), this.getMonth());
formFeed() ;
}
/**
* 打印对应月份的日历
* @param year 年份
* @param month 月份
*/
public void getTable(int year, int month) {
calendar = new MyCalendar(year, month) ;
calendar.table();
System.out.println();
}
/**
* 打印本月日历
*/
public void thisMonth() {
calendar = new MyCalendar() ;
calendar.table();
formFeed() ;
}
/**
* 实现日历的翻页功能
*/
public void formFeed() {
int ch = 0 ;
do {
System.out.println("0.返回 1.上个月 2.下个月 其它数字退出。");
ch = inputInt() ;
switch(ch) {
case 0:
break ;
case 1:
if(this.getMonth() == 1) {
this.setMonth(12);
this.setYear(this.getYear() - 1);
}else {
this.setMonth(this.getMonth() -1);
}
break ;
case 2:
if(this.getMonth() == 12) {
this.setMonth(1);
this.setYear(this.getYear() + 1);
}else {
this.setMonth(this.getMonth() + 1);
}
break ;
default:
System.out.println("\n程序已退出,谢谢使用!");
System.exit(0);
}
getTable(this.getYear(), this.getMonth());
}while(ch != 0) ;
}
//整形输入检测
public int inputInt() {
int num = 0 ;
while(!input.hasNextInt()){
System.out.println("输入有误请重新输入!");
input.next();
}
num = input.nextInt() ;
return num ;
}
}
客户端程序(main方法)
public class Run {
public static void main(String[] args) {
// TODO Auto-generated method stub
UI ui = new UI() ;
CalendarProgram cp = new CalendarProgram() ;
Scanner input = new Scanner(System.in) ;
int ch = 0 ;
do {
ui.u_1();
System.out.println("请输入所需功能对应的值:");
ch = cp.inputInt() ;
switch (ch) {
case 0:
break ;
case 1:
cp.thisMonth();
break ;
case 2:
cp.search();
break ;
default:
System.out.println("无此选项!");
break;
}
} while (ch != 0);
System.out.println("\n程序已退出,谢谢使用!");
}
}
运行结果