假设已知定义了一个如下的类

来源:百度知道 编辑:UC知道 时间:2024/05/22 13:13:15
public class my catendar {
private int year.month.day;
public my calendar tomorrow(){}
public my calendar yesterday(){}
} 请写出实现tomorrow 和yesterday()方法的具体实现
是一道java 的编程题目 帮忙解一下 我要交的作业呀

对不起, 我对JAVA的程序风格不怎么记得了,
反正你要的是算法是吧,

下面你应该能看得懂吧

public MyCalendar tomorrow()
{
int y = this.year, m = this.month, d = this.day; // 首先取得当前 MyCalendar 的日期
int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31}; // 每个月的天数

if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) daysInMonth[2] = 29; // 闰年的话, 2月将有 29 天

if (d == daysInMonth[m - 1]) // 月末, 因为是月末, 所以该日期的下一天就是下月的一号
{
d = 1; // 设日为一号
if (m == 12) // 年底, 此时日期就类似 2006 年 12 月 31 日, 那么该日期的下一天就是 2007 年月 1 月 1 日
{
m = 1; // 月数为 1
y++; // 年数加 1
}
else // 既然不是年底, 那么直接月数加 1
{
m++;
}
}
else // 既然不是月末, 只要日加 1 就可以得到下一天
{
d++;
}

return new MyCalendar(y, m, d); // 返回一个新的 MyCalendar 对象, 其日期是当前 MyCalendar 对象的下一天
}

public MyCalendar yesterday()
{
int y = this.year, m = this.month, d = this.day; // 首先取得当前 MyCalendar