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

📄 date.java

📁 清华大学第二版
💻 JAVA
字号:
//程序5-2
public class Date {
	private int day, month, year;
	Date ( int i, int j, int k) {
		day = i;
		month = j;
		year = k;
	}

	Date() {		//构造方法
		day = 1;
		month = 1;
		year = 1998;
	}

	Date ( Date d) {//	带一个参数的构造方法
		day = d.day;
		month = d.month;
		year = d.year;
	}

	public void printDate() {
		System.out.print(day + "/"  + month + "/" + year);
	}

	public Date tomorrow() {
		Date d = new Date(this);
		d.day++;
		if (d.day > d.daysInMonth()) {
			d.day = 1;
			d.month ++;
			if (d.month > 12) {
				d.month = 1;
				d.year ++;
			}
		}
		return d;
	}

	public int daysInMonth() {
		switch (month) {
		case 1: case 3: case 5: case 7: 
		case 8: case 10: case 12:
			return 31;
		case 4: case 6: case 9: case 11:
			return 30;
		default:
			if ( year % 100 != 0 && year % 4 == 0 ) {
				return 29;
			}
			else return 28;
		}
	}
	
	public static void main (String args[]) {
		Date d1 = new Date();
		System.out.print("The current date is (dd / mm / yy): ");
		d1.printDate();
		System.out.println();
		System.out.print("Its tomorrow is (dd / mm / yy): ");
		d1.tomorrow().printDate();
		System.out.println();

		Date d2 = new Date(28, 2, 1965);
		System.out.print("The current date is (dd / mm / yy): ");
		d2.printDate();
		System.out.println();
		System.out.print("Its tomorrow is (dd / mm / yy): ");
		d2.tomorrow().printDate();
		System.out.println();
	}
}

⌨️ 快捷键说明

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