NextDay源代码
package yanhongyun;
public class NextDay
{
private int year;
private int month;
private int day;
public NextDay()
{
}
public NextDay(int year,int month,int day)
{
set(year,month,day);
}
public void set(int year,int month,int day)
{
if (year<0)
this.year=0;
else
this.year=year;
if (month<1 || month>12)
this.month=1;
else
this.month=month;
if (day<1 || day>lastDay(year,month))
this.day=1;
else
this.day=day;
}
private int lastDay(int year,int month)
{
if (month==1 || month==3 || month==5 ||month==7 || month==8 || month==10||month==12)
{
return 31;
}
else if (month==2)
{
if ((year%4==0 && year%100!=0) || year %400==0)
return 29;
else
return 28;
}
else
{
return 30;
}
}
public NextDay nextDay()
{
day=day+1;
if (day>lastDay(year,month))
{
day=1;
month=month+1;
if (month>12)
{
month=1;
year=year+1;
}
}
return this;
}
public boolean equals(Object obj)
{
if (obj instanceof NextDay)
{
NextDay nd=(NextDay)obj;
return (nd.year==this.year && nd.month==this.month && nd.day==this.day);
}
else
return false;
}
public String tndString()
{
String str=year+"年"+month+"月"+day+"日";
return str;
}
}
Junit 测试程序
package yanhongyun;
import junit.framework.Assert;
import junit.framework.TestCase;
public class TestNextDay extends TestCase
{
NextDay date1;
NextDay date2;
public TestNextDay(String name)
{
super(name);
}
public void setUp()
{
}
public void testSet()
{
date1=new NextDay(2000,2,30);
date2=new NextDay(2000,2,1);
Assert.assertEquals(date2,date1);
date1.set(1969,8,29);
assertEquals(date1,date1);
date1.set(1991,2,29);
assertEquals(new NextDay(1991,2,1),date1);
date1.set(-120,2,1);
assertEquals(new NextDay(0,2,1),date1);
date1.set(-120,-2,-1);
assertEquals(new NextDay(0,1,1),date1);
}
public void testNextDay()
{
date1=new NextDay(1992,12,31);
date2=new NextDay(1993,1,1);
assertEquals(date2,date1.nextDay());
date1.set(1992,2,28);
date2.set(1992,2,29);
assertEquals(date2,date1.nextDay());
date1.set(1992,2,29);
date2.set(1992,3,1);
assertEquals(date2,date1.nextDay());
date1.set(1998,2,28);
date2.set(1998,3,1);
assertEquals(date2,date1.nextDay());
date1.set(1995,1,19);
date2.set(1995,1,20);
assertEquals(date2,date1.nextDay());
date1.set(2006,1,31);
date2.set(2006,2,1);
assertEquals(date2,date1.nextDay());
date1.set(2006,4,30);
date2.set(2006,5,1);
assertEquals(date2,date1.nextDay());
}
public static void main(String []args)
{
junit.textui.TestRunner.run(TestNextDay.class);
}
}