在包Calendar下的类
日历类
package Calendar;
import java.util.Calendar;
//日历类
public class CalendarBean {
int year=0;
int month=0;
public CalendarBean() {
Date date = new Date();//默认为当前系统日期
String year = String.format("%tY", date);//提取年
String month = String.format("%tm", date);//提取日
int y = Integer.parseInt(year);//转换为int型
int m = Integer.parseInt(month);//转换为int型
this.year = y;
this.month = m;
}
public void setYear(int year) {//设置年
this.year = year;
}
public void setMonth(int month) {//设置月
this.month = month;
}
public String [] getCalendar() {//生成日历,存储在String数组中
String [] a = new String[42];//存储日期的String数组
Calendar cal = Calendar.getInstance();
cal.set(year,month-1,1);//初始化某年某日的日历
/*
* cal.set(int year, int month, int date)中month=0代表1月。
* year取负表示公元前
*/
int weekDay = cal.get(Calendar.DAY_OF_WEEK)-1;//获取某年某月1号对应的星期
/*
* cal.get(Calendat.DAY_OF_WEEK)返回一个整数,1代表星期日,2代表星期一,3代表星期二.....
*/
int day = 0;//某月对应的天数
switch(month) {//判断某月的天数
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day = 31;
break;
case 4: case 6: case 9: case 11:
day = 30;
break;
case 2://2月份平年闰年的天数不同,需判断年
if((year%4==0)&&(year%100!=0)||(year%400==0)) {//判断闰年
day = 29;
break;
}
else
day = 28;
break;
}
for(int i=0;i<weekDay;i++) {//1号对应的星期之前全为空格
a[i]=" ";
}
for(int i=weekDay,n=1;i<weekDay+day;i++) {//填写1号到本月最后一天
a[i] = String.valueOf(n);
n++;
}
for(int i=weekDay+day;i<a.length;i++) {//本月最后一天之后全为空格
a[i] = " ";
}
return a;
}
public void Previous() {//查看本月之前的月份
if(month==1) {//Month为1时的处理
this.year = year-1;
this.month = 12;
}
else {
this.month = month-1;
}
}
public void Next() {//查看本月之后的月份
if(month==12) {//month为12时的处理
this.year = year+1;
this.month = 1;
}
else {
this.month = month+1;
}
}
public void Date() {//输出年月
System.out.println(year+"年"+month+"月");
}
}
打印日历
package Calendar;
import java.util.Scanner;
//打印出日历,输入p时查看之前的月份,输入n后产看之后的月份
public class Calendars {
public static void main(String args[]) {
CalendarBean cab = new CalendarBean();
String [] week = {"日","一","二","三","四","五","六"};
String [] a = cab.getCalendar();
//打印默认年月
cab.Date();//输出此时的年月
for(int i=0;i<7;i++) {//输出星期
if(i==6) {
System.out.printf("%s\t",week[i]);
}
else {
System.out.printf("%s\t",week[i]);
}
}
for(int i=0;i<a.length;i++) {//输出日历
if(i%7==0) {
System.out.println();
}
System.out.printf("%s\t",a[i]);
}
System.out.println();
System.out.println("previous(p) or next(n) month:");
//根据条件打印日历
while(true) {
String s = new Scanner(System.in).nextLine();
if(s.equals("set")) {//打印自定义日历
int year = new Scanner(System.in).nextInt();
int month = new Scanner(System.in).nextInt();
cab.setYear(year);
cab.setMonth(month);
cab.Date();
a = cab.getCalendar();
for(int i=0;i<7;i++) {
if(i==6) {
System.out.printf("%s\t",week[i]);
}
else {
System.out.printf("%s\t",week[i]);
}
}
for(int i=0;i<a.length;i++) {
if(i%7==0) {
System.out.println();
}
System.out.printf("%s\t",a[i]);
}
System.out.println();
System.out.println("previous(p) or next(n) month:");
}
if(s.equals("p")) {//打印本月之前的月份的日历
cab.Previous();
a = cab.getCalendar();
cab.Date();
for(int i=0;i<7;i++) {
if(i==6) {
System.out.printf("%s\t",week[i]);
}
else {
System.out.printf("%s\t",week[i]);
}
}
for(int i=0;i<a.length;i++) {
if(i%7==0) {
System.out.println();
}
System.out.printf("%s\t",a[i]);
}
System.out.println();
System.out.println("previous(p) or next(n) month:");
}
if(s.equals("n")){//打印本月之后的日历
cab.Next();
cab.Date();
a = cab.getCalendar();
for(int i=0;i<7;i++) {
if(i==6) {
System.out.printf("%s\t",week[i]);
}
else {
System.out.printf("%s\t",week[i]);
}
}
for(int i=0;i<a.length;i++) {
if(i%7==0) {
System.out.println();
}
System.out.printf("%s\t",a[i]);
}
System.out.println();
System.out.println("previous(p) or next(n) month:");
}
if(s.equals("e")) {//退出程序
break;
}
}
}
}