計算兩個年份之間的天數

class SumDays{ 	    
    public static void main(String[] args) {
        String startDate = "20120101";
        String endDate = "20130101";
        SumDaystCalendar = new SumDays();
        tCalendar.initDayInfo(startDate, endDate);
    }
         public  void initDayInfo(String start,String end)
    {
    	//初始化日期信息
    	java.util.Calendar cal1=java.util.Calendar.getInstance();
    	java.util.Calendar cal2=java.util.Calendar.getInstance();
    	java.util.Calendar cal3=java.util.Calendar.getInstance();
    	int year,month,day;
    	int i=0;
    	year=Integer.parseInt(start.substring(0,4));
    	month=Integer.parseInt(start.substring(4,6));
    	day=Integer.parseInt(start.substring(6,8));
    	cal1.set(year, month-1, day);
    	cal3.set(year, month-1, day);
    	year=Integer.parseInt(end.substring(0,4));
    	month=Integer.parseInt(end.substring(4,6));
    	day=Integer.parseInt(end.substring(6,8));
    	cal2.set(year, month-1, day);
    	while(!cal2.before(cal3))
    	{	
    		i++;
    		cal3.add(java.util.Calendar.DAY_OF_MONTH, 1);//日期時間+1
    	}
    	System.out.println(「總天數:」+i);
    }
}
相關文章
相關標籤/搜索