package com.tan.test314Date.homework;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 制作万年历
*
* @use
* @author tanyouhao
* @date 2013-3-14 下午4:07:53
* @version 1.0
*
*/
public class CalendarTest {
/**
* 返回两个日期之间的条数
*
* @param date1
* 第一个日期 格式为:2013-03-13
* @param date2
* 第二个日期
* @return
* @throws ParseException
*/
public static int getDaysByTwoDate(int year, int month, int day)
throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date da1 = format.parse("1900-01-01");
String date2 = "";
if (day == 0) {
date2 = year + "-" + month + "-01";
} else {
date2 = year + "-" + month + "-" + day;
}
Date da2 = format.parse(date2);
long times = da2.getTime() - da1.getTime();
times = times / (1000 * 60 * 60 * 24);
return Integer.parseInt(String.valueOf(times));
}
/**
* 根据年月输出当月一共多少天
*
* @param year
* 年份实例 1990
* @param month
* 月份 实例 2
* @return
* @throws ParseException
*/
public static int getDaysByDate(int year, int month) throws ParseException {
int days = 0;
if (month == 2) {
if (isRun(year)) {
days = 29;
} else {
days = 28;
}
} else {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
default:
days = 30;
break;
}
}
return days;
}
/**
* 判断一个年是否为闰年
*
* @param year
* @return true为是 false为否
*/
public static boolean isRun(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
/**
* @throws ParseException
* 根据年月创建日历
* @param year
* @param month
* @throws
*/
public static void createCalendar(int year, int month)
throws ParseException {
int lastShowDate=0;//主要是用来记录这个月最后一天如果是周六则不显示下个月,如果不是则显示下个月 把该行显示满
int daysTwoDate = getDaysByTwoDate(year, month, 0);
int daysForMonth = getDaysByDate(year, month);
int week = daysTwoDate % 7 + 1;
System.out.println(" " + year + "年" + month + "月");
System.out.println(" 日 " + " 一 " + " 二 " + " 三 " + " 四 " + " 五 "
+ " 六 ");
int day = 0;
int wee = 0;
for (int i = 1; i <= daysForMonth; i++) {
day = getDaysByTwoDate(year, month, i);
wee = (day + 1) % 7;// 该月i日的星期几
if (i == 1) {
//获取上个月天数
int dayLast=getDayForLastMonth(year, month);
int daybegin=dayLast-week+1;
for (int j = 0; j < wee; j++) {
System.out.print(" " +daybegin);
daybegin++;
// System.out.print(" ");
}
}
if (i < 10) {
System.out.print(" " + i);
} else {
System.out.print(" " + i);
}
if (wee == 6) {
System.out.println();
}
lastShowDate=wee;
}
if(lastShowDate!=6){
int m=6-lastShowDate;
for(int i=1;i<=m;i++){
System.out.print(" " + i);
}
}
//
}
/**
* 获取上一个月的天数
*
* @param year
* @param month
* @return
* @throws ParseException
*/
public static int getDayForLastMonth(int year, int month)
throws ParseException {
int days = 0;
if (month == 1) {
year--;
month = 12;
} else {
month--;
}
days = getDaysByDate(year, month);
return days;
}
/**
* 获取下一个月天数
*
* @param year
* @param month
* @return
* @throws ParseException
*/
public static int getDaysForNextMonth(int year, int month)
throws ParseException {
int days = 0;
if (month == 12) {
year++;
month = 1;
} else {
month++;
}
days = getDaysByDate(year, month);
return days;
}
public static void main(String args[]) throws ParseException {
createCalendar(2014, 12);
}
}
运行结果:
2014年12月
日 一 二 三 四 五 六
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
相关函数请查询API!