JAVA取出特定年月的最大日期

public class ObautDate {   
  /**     * @param args     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int lastDate = 0;
        lastDate = lastDate(2009,2);
        System.out.println("lastDate="+lastDate);
    }
//判斷是不是閏年
    public static boolean isLeapYear(int year){
        if (year % 4 != 0) 
           return false;
          if (year % 400 == 0) 
           return true;
          return year % 100 != 0;
       }       

//取出最大日期
    public static int lastDate(int year,int month){
        int kLastDates[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if(month>12 || month<0){
            month = 0;
        }
        if(month==2 && isLeapYear(year)){
            return kLastDates[month] + 1;
        }else{
            return kLastDates[month]; 
       }
    }
}

發佈了22 篇原創文章 · 獲贊 2 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章