这次尝试更具体。
总体思路:
我无法获得课程日期以允许我提供返回类型日期。
接口代码:
public interface elapsed
{
/*
* a year can have 365 or 366 days
* depending on "leap-year".
* February can have 28 or 29 days
* also depending on "leap-year"
*/
public static final int days_week = 7;
public static final int weeks_year = 52;
//add any number of days to a date
public date addDays (int d);
//subtract any number of days from a day
public date subDays (int d);
}
所以课程日期必须使用subDays和addDays。好。我决定不使用subDays,因为我可以翻转为整数d输入的值,并使用相同的数学。
所以我的班级日期如下:
public class date implements elapsed
{
int day;
int month;
int year;
public date (int Day, int Month, int Year)
{
day = Day;
month = Month;
year = Year;
}
public date ()
{
}
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public date addDays (int x)
{
int d = getDay();
int m = getMonth();
int y = getYear();
m = (m + 9) % 12;
y = y - (m / 10);
int g = (365 * y) + (y / 4) - (y / 100) + (y / 400) + (( m * 306 + 5) / 10) + ( d - 1 );
g = g + x; //adds days to the g variable which is the date from clock start
y = (10000 * g + 14780)/3652425;
int ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
if (ddd < 0) //manages the occurrences of leap years
{
y = y - 1;
ddd = g - (365*y + y/4 - y/100 + y/400);
}
int mi = (100 * ddd + 52) / 3060;
int mm = (mi + 2) % 12 + 1;
y = y + (mi + 2) / 12;
int dd = ddd - (mi * 306 + 5) / 10 + 1;
return new date(dd, mm, y);
}
public date subDays (int x)
{
int d = getDay();
int m = getMonth();
int y = getYear();
m = (m + 9) % 12;
y = y - (m / 10);
int g = (365 * y) + (y / 4) - (y / 100) + (y / 400) + (( m * 306 + 5) / 10) + ( d - 1 );
g = g - x; //subtracts days to the g variable which is the date from clock start
y = (10000 * g + 14780)/3652425;
int ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
if (ddd < 0) //manages the occurrences of leap years
{
y = y - 1;
ddd = g - (365*y + y/4 - y/100 + y/400);
}
int mi = (100 * ddd + 52) / 3060;
int mm = (mi + 2) % 12 + 1;
y = y + (mi + 2) / 12;
int dd = ddd - (mi * 306 + 5) / 10 + 1;
return new date(dd, mm, y);
}
public String toString(int day, int month, int year) // formats the date into something nicer
{
String formMonth = null;
switch(month)
{
case 1:
formMonth = "Jan";
break;
case 2:
formMonth = "Feb";
break;
case 3:
formMonth = "Mar";
break;
case 4:
formMonth = "Apr";
break;
case 5:
formMonth = "May";
break;
case 6:
formMonth = "Jun";
break;
case 7:
formMonth = "Jul";
break;
case 8:
formMonth = "Aug";
break;
case 9:
formMonth = "Sep";
break;
case 10:
formMonth = "Oct";
break;
case 11:
formMonth = "Nov";
break;
case 12:
formMonth = "Dec";
break;
default:
System.exit(0);
}
String formDate = day + formMonth + year;
return formDate;
}
} //class end brackets
我需要addDays来返回日期,但我无法弄清楚如何使用它或使用什么语法。我觉得界面没有正确地给我们,但我知道什么。
我只需要它来渲染一个新的日期,以便在我的主要内容中我可以使用类似的东西 日期d2 = d1.addDays(x);
将它放入我必须构建的tostring并使其看起来很漂亮。
这个问题更有意义吗?
感谢您的帮助。
答案 0 :(得分:0)
我不是100%肯定你在这里问的是什么,所以这更像是一个疯狂的猜测,但我有点自信。
在addDays
方法中,您要创建并返回新日期。您可以通过从当前日期获取数据并将给定天数添加到这些值,然后调用date
构造函数来返回新实例。到目前为止,你走在正确的轨道上!唯一的问题似乎是,构造函数不是像this.date(...)
那样被调用的实例的方法,但必须像new date(...)
一样调用。
public date addDays (int x) {
int d = getDay();
int m = getMonth();
int y = getYear();
// date adding logic, did not check, assuming to be correct
return new date(dd, mm, y);
}
答案 1 :(得分:0)
如果您只想让addDays()返回日期,那么只需执行以下操作:
public date addDays(int x)
{
return new date(dd,mm,yy);
}
也
public String toString()
{
}