7-1 MyDate类

构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0 在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果

输入格式:

两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日

输出格式:

当前日期对象的信息,当前对象和待比较日期对象的比较结果

输入样例:

在这里给出一组输入。例如:

2008 6 12 2009 6 22

输出样例:

在这里给出相应的输出。例如:

6/12/2008 -1

通过代码:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MyDate d1 = new MyDate(sc.nextInt(), sc.nextInt(), sc.nextInt());
		MyDate d2 = new MyDate(sc.nextInt(), sc.nextInt(), sc.nextInt());
		d1.print();
		System.out.print(" ");
		System.out.println(d1.compare(d2));
		sc.close();
	}
}

class MyDate{
	private int y, m, d;//年、月、日

	public MyDate(int y, int m, int d) {
		this.y = y;
		this.m = m;
		this.d = d;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getM() {
		return m;
	}

	public void setM(int m) {
		this.m = m;
	}

	public int getD() {
		return d;
	}

	public void setD(int d) {
		this.d = d;
	}
	
	public void print() {
		System.out.print(this.m+"/"+this.d+"/"+this.y);
	}
	
	int compare(MyDate d) {
		if(this.y < d.getY()) return -1;
		else if(this.y > d.getY()) return 1;
		if(this.m < d.getM()) return -1;
		else if(this.m > d.getM()) return 1;
		if(this.d < d.getD()) return -1;
		else if(this.d > d.getD()) return 1;
		return 0;
	}
}

 

好的,我了解了您的问题。让我来回答您的问题。 MyDate 是一个日期,它可以存储年、月、日的信息,并且能够对日期进行一些操作。下面是完善后的 MyDate 的代码示例: ```java public class MyDate { private int year; private int month; private int day; public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDay(int day) { this.day = day; } public void setDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public String toString() { return String.format("%04d-%02d-%02d", year, month, day); } public boolean equals(Object obj) { if (obj instanceof MyDate) { MyDate date = (MyDate) obj; return year == date.year && month == date.month && day == date.day; } return false; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + day; result = prime * result + month; result = prime * result + year; return result; } public MyDate nextDay() { int nextDay = day + 1; int nextMonth = month; int nextYear = year; if (nextDay > getDaysInMonth(year, month)) { nextDay = 1; nextMonth++; if (nextMonth > 12) { nextMonth = 1; nextYear++; } } return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextMonth() { int nextMonth = month + 1; int nextYear = year; if (nextMonth > 12) { nextMonth = 1; nextYear++; } int maxDay = getDaysInMonth(nextYear, nextMonth); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, nextMonth, nextDay); } public MyDate nextYear() { int nextYear = year + 1; int maxDay = getDaysInMonth(nextYear, month); int nextDay = Math.min(day, maxDay); return new MyDate(nextYear, month, nextDay); } private int getDaysInMonth(int year, int month) { 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; case 2: if (isLeapYear(year)) { return 29; } else { return 28; } default: throw new IllegalArgumentException("Invalid month: " + month); } } private boolean isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } } } ``` 在这个中,我们实现了以下方法: - 构造方法 MyDate(int year, int month, int day):用于创建一个日期对象。 - getYear()、getMonth()、getDay():用于获取年、月、日。 - setYear()、setMonth()、setDay():用于设置年、月、日。 - setDate(int year, int month, int day):用于设置年、月、日。 - toString():用于以字符串形式返回当前日期。 - equals(Object obj):用于判断当前日期是否与另一个日期相等。 - hashCode():用于生成当前日期的哈希码。 - nextDay()、nextMonth()、nextYear():用于获取当前日期的下一天、下一个月、下一年。 - getDaysInMonth(int year, int month):用于获取指定年月的天数。 - isLeapYear(int year):用于判断指定年是否为闰年。 这些方法可以帮助我们对日期进行各种操作,比如获取下一天的日期、判断两个日期是否相等等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值