友帮技术博客

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

计算输入日期的下一天的nextDate()

同样是软件测试课程的作业,测试过程不解释,贴图贴代码。

 

 

 

 

 

代码
public static ArrayList NextDate(int nowYear, int nowMonth, int nowDay)
    {
        ArrayList arr 
= new ArrayList();
        
if (nowYear <= 3000 && nowYear >= 1000 && nowMonth <= 12 && nowMonth >= 1 && nowDay >= 1 && nowDay <= getDays(nowYear, nowMonth))
        {
            
int nextYear;
            
int nextMonth;
            
int nextDay;
            
if (nowDay == getDays(nowYear, nowMonth))
            {
                
if (nowMonth == 12)
                {
                    nextYear 
= nowYear + 1;
                    nextMonth 
= 1;
                    nextDay 
= 1;
                }
                
else
                {
                    nextYear 
= nowYear;
                    nextMonth 
= nowMonth + 1;
                    nextDay 
= 1;
                }
            }
            
else
            {
                nextYear 
= nowYear;
                nextMonth 
= nowMonth;
                nextDay 
= nowDay + 1;
            }
            arr.Add(nextYear);
            arr.Add(nextMonth);
            arr.Add(nextDay);
        }
        
else
        {
            arr.Add(
"错误年份");
            arr.Add(
"错误月份");
            arr.Add(
"错误天数");
        }
        
return arr;
    }

    
protected static bool isRun(int year)
    {
        
if (year % 4 == 0 && year % 400 != 0)
        {
            
return true;
        }
        
return false;
    }

    
protected static int getDays(int year, int month)
    {
        
int days = 0;
        
switch (month)
        {
            
case 1:
            
case 3:
            
case 5:
            
case 7:
            
case 8:
            
case 10:
            
case 12: days = 31break;
            
case 2: days = isRun(year) ? 29 : 28break;
            
case 4:
            
case 6:
            
case 9:
            
case 11: days = 30break;
        }
        
return days;
    }

 

 

posted on 2010-12-21 09:39  友帮  阅读(1149)  评论(0)    收藏  举报