利用java的Date和Calendar类编写自己的日历

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值