从某年某月的某一天到另一个某年某月的某一天

用java输出,从某年某月的某一天到另一个某年某月的某一天的,中间的所有日子,和判断是周几。

看代码吧

public class Test2 {

	/**
	 * 从year1年month2月到year2年到month2月的
	 * 
	 * @param args
	 */
	int days = 0;

	public static void main(String[] args) {
		int year1= 2013;
		int year2 = 2014;

		int month1 = 2;
		int month2 = 1;
		int day1 = 3;
		int day2 = 24;

		Test2 t = new Test2();
		t.printDay(year1, month1,day1, year2, month2,day2);

	}

	public void printDay(int year1, int month1,int day1, int year2, int month2,int day2) {
		int sum = 0;
		int sum1 = 0;
		int sum2 = 0;
		int year=year1;
		for (; year <= year2;year++ ) {

			int m = month1;
			int m2 = 12;
			// 第二年要从1月开始循环
			if (year >year1) {
				m=1;
				//	m2 = month2;
			}
			//循环的年份和终止的年份相同时,到终止年份的月份终止循环
			if(year == year2){
				m2 = month2;
			}
			for (; m <= m2; m++) {
				// 求这个月的第一天是这一年的第几天;
				sum1=month(year,m);

				// 求从1900.01.01开始过到咯第几天;
				for (int j = 1900; j < year; j++) {

					if (j % 4 == 0 && j % 100 != 0 || j % 400 == 0) {
						sum2 += 366;
					} else {
						sum2 += 365;
					}
				}
				sum = sum1 + sum2;

				System.out.println("——————————————" + year + "  " + m+ "—————————————————————————");

				int a=1;
				if (year == year2&&m==month2) {
					//m=1;
					//	m2 = month2;

					days=day2;
				}

				if(year==year1&&m==month1){
					 a=day1;
				}
				for (; a <= days; a++) {
					//b代表星期几,0是星期日
					int b = (sum + a) % 7;
					System.out.print(a+" "+ b+ "\t");
					if ((sum + a) % 7 == 6) {
						System.out.println();
					}
				}

				sum1 = 0;
				sum2 = 0;
				sum = 0;
				System.out.println("\n");

			}

		}
	}

	public int month(int year, int month) {
		int summ = 0;
		for (int i = 1; i <= month; i++) {
			// 判断每月的天数;
			switch (i) {
			case 2:
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					days = 29;
				} else {
					days = 28;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
				break;
			default:
				days = 31;
				break;
			}
			if (i < month) {
				summ += days;
			}
		}
		System.out.println(summ);
		return summ;

	}

}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

疑问 开心 悲伤 邪恶 惊叹 微笑 脸红 笑 惊讶 惊奇 迷惑 酷 憨笑 生气 阴险 转眼球 眨眼 主意 箭头 中立 哭 大笑