乔恩,谢谢,你说得对,让我附上类中缺少的 next() 方法:
public date Next(date d)
{
if (!d.valid()) return new date();
date ndat = new date((d.Day() + 1), d.Month(), d.Year());
if (ndat.valid()) return ndat;
ndat = new date(1, (d.Month() + 1), d.Year());
if (ndat.valid()) return ndat;
ndat = new date(1, 1, (d.Year() + 1));
return ndat;
}
由于这使用了 valid() 我也会附上这个:
public bool valid()
{
// This method will check the given date is valid or not.
// If the date is not valid then it will return the value false.
if (year < 0) return false;
if (month > 12 || month < 1) return false;
if (day > 31 || day < 1) return false;
if ((day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)))
return false;
if (day == 30 && month == 2) return false;
if (day == 29 && month == 2 && (year % 4) != 0) return false;
if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false;
/* ASIDE. The duration of a solar year is slightly less than 365.25 days. Therefore,
years that are evenly divisible by 100 are NOT leap years, unless they are also
evenly divisible by 400, in which case they are leap years. */
return true;
}
我认为 Day()、Month() 和 Year() 是不言自明的,但如果需要,请告诉我。我还有一个 previous() 方法,它与我想在 --decrement 方法中使用的 next() 相反。
现在在我的程序中,我有
class Program
{
static void Main()
{
date today = new date(7,10,1985);
date tomoz = new date();
tomorrow = today++;
tomorrow.Print(); // prints "7/10/1985" i.e. doesn't increment
Console.Read();
}
}
所以它实际上并没有失败,它只是打印今天的日期而不是明天的,但如果我使用 ++today 代替,它可以正常工作。
关于 D/M/Y 的顺序,是的,我同意,通过更高频率的数据,我可以看到它如何改善事情,接下来我将继续修复它。