⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day.java

📁 a new calendar using gregorian calendar class
💻 JAVA
字号:
public class Day {
	private int year;
	private int month;
	private int date;

	private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31,
			31, 30, 31, 30, 31 };

	private static final int GREG_START_YEAR = 1582;
	private static final int GREG_START_MONTH = 10;
	private static final int GREG_START_DAY = 15;
	private static final int JULIAN_END_DAY = 4;

	public static final int JAN = 1;
	public static final int FEB = 2;
	public static final int DEC = 12;

	//Costruttore della classe
	public Day(int year, int month, int date) {
		this.year = year;
		this.month = month;
		this.date = date;
	}

	//metodi Get
	public int getYear() {
		return year;
	}
	public int getMonth() {
		return month;
	}
	public int getDate() {
		return date;
	}

	//aggiunta di giorni
	public Day addDays(int n) {
		Day risultato = this;
		while (n > 0) {
			risultato = risultato.nextDay();
			n--;
		}
		while (n < 0) {
			risultato = risultato.prevDay();
			n++;
		}
		return risultato;
	}

	//conteggio tra un giorno e un altro
	public int daysFrom(Day other) {
		int n = 0;
		Day d = this;
		while (d.compareTo(other) > 0) {
			d = d.prevDay();
			n++;
		}
		while (d.compareTo(other) < 0) {
			d = d.nextDay();
			n--;
		}
		return n;
	}

	private int compareTo(Day other) {
		if (year > other.year)
			return 1;
		if (year < other.year)
			return -1;
		if (month > other.month)
			return 1;
		if (month < other.month)
			return -1;
		return date - other.date;
	}
	private Day nextDay() {
		int y = year;
		int m = month;
		int d = date;

		if (y == GREG_START_YEAR && m == GREG_START_MONTH
				&& d == JULIAN_END_DAY)
			d = GREG_START_DAY;
		else if (d < daysPerMonth(y, m))
			d++;
		else {
			d = 1;
			m++;
			if (m > DEC) {
				m = JAN;
				y++;
				if (y == 0)
					y++;
			}
		}
		return new Day(y, m, d);
	}
	private Day prevDay() {
		int y = year;
		int m = month;
		int d = date;

		if (y == GREG_START_YEAR && m == GREG_START_MONTH
				&& d == GREG_START_DAY)
			d = JULIAN_END_DAY;
		else if (d > 1)
			d--;
		else {
			m--;
			if (m < JAN) {
				m = DEC;
				y--;
				if (y == 0)
					y = -1;
			}
			d = daysPerMonth(y, m);
		}
		return new Day(y, m, d);
	}
	private static int daysPerMonth(int y, int m) {
		int days = DAYS_PER_MONTH[m - 1];
		if (m == FEB && isLeapYear(y))
			days++;
		return days;
	}

	private static boolean isLeapYear(int y) {
		if (y % 4 != 0)
			return false;
		if (y < GREG_START_YEAR)
			return true;
		return (y % 100 != 0) || (y % 400 == 0);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -