首页>代码>java swing日历calendar demo源代码下载>/calanders/src/can/lyj/com/Mycalander.java
package can.lyj.com;

public class Mycalander {

	/**
	 * 
	 * @param year
	 *            年份
	 * @return
	 */
	private static boolean isLoopYear(int year) {

		return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

	}

	/**
	 * 
	 * @param year
	 *            年份
	 * @return 获得当前日期的第一天是星期几
	 */
	private static int getWeek(int year, int month) {
		long days = 0, sum = 0;
		final int MAX = 366, MIN = 365, Day_OF_WEEK = 7;

		for (int y = 1; y < year; y++) {
			days = isLoopYear(y) ? MAX : MIN;
			sum += days;
		}
		for (int mon = 1; mon < month; mon++) {
			if (mon != 2) {
				int dy = isBig(mon) ? 31 : 30;
				sum += dy;

			} else if (mon == 2) {
				sum += isLoopYear(year) ? 29 : 28;
			}
		}
		int week = (int) ((sum + 1) % Day_OF_WEEK);
		return week;

	}

	/**
	 * 
	 * @param month月份
	 *            判断是大月还是小月
	 */
	private static boolean isBig(int month) {
		switch (month) {
		case 1:case 3:case 5:case 7:case 8 : case 10 :case 12:
			return true;

		default:
			return false;
		}
	}

	private static int getHowDay(int currMonth, int year) {
		if (currMonth == 2) {
			if (isLoopYear(year)) {
				return 29;
			} else
				return 28;
		}
		return isBig(currMonth) ? 31 : 30;
	}

	/**
	 * 根据年月获得日历数组
	 * 
	 * @param years
	 * @param months
	 * @return
	 */
	public static int[][] getDateAry(int years, int months) {
		int[][] date = new int[6][7];
		int howDays = getHowDay(months, years);
		final int before=getHowDay( months==1?12:months-1,months==1?years-1:years);
		int howWeek = getWeek(years, months);
		int first=before-howWeek+1;
		int index = 0;
		int priveous=1;
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7 ; j++) {
				if (i == 0) {
					if (j < howWeek) {
						date[0][j]=first++;
					} else {
						date[0][j] = ++index;
					}

				} else if( index <howDays){
					date[i][j] = ++index;
				}
				else{
					date[i][j]=priveous;
					priveous++;
				}
				
			}

		}
		return date;

	}

}
最近下载更多
wadadd  LV7 2022年9月5日
Demo1111  LV30 2021年12月12日
skyer5217  LV2 2020年12月23日
taqinchuan  LV1 2020年8月2日
admin_z  LV22 2020年5月29日
xuexue123  LV1 2020年5月28日
116699  LV1 2020年3月30日
小强111111  LV20 2020年2月15日
yongzheng132  LV17 2019年6月24日
寸头的春天  LV1 2019年5月11日
最近浏览更多
woldxy  LV12 4月1日
Ji123455  LV8 2023年9月21日
SILKYYY 2023年5月25日
暂无贡献等级
xuexizhuanyong23  LV16 2023年4月29日
qq1061521319  LV4 2022年11月14日
香菇肉饼汤  LV8 2022年10月30日
3233497907  LV1 2022年10月21日
alonely23  LV1 2022年10月20日
wadadd  LV7 2022年9月5日
1803466516  LV4 2022年7月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友