MENU

Java 월말 날짜 얻기

목차

자바 월말일얻기 Calendar.getActualMaximum()

자바에서 지정한 날짜월말일취득하는 샘플 프로그램입니다.
java.util.Calendar수업getActualMaximum(Calendar.DATE)월말일을 얻을 수 있습니다.

샘플 프로그램

/**
 * 指定した日付文字列(yyyy/MM/dd or yyyy-MM-dd)
 * における月末日付を返します。
 * 
 * @param strDate 対象の日付文字列
 * @return 月末日付
 */
public static int getLastDay(String strDate) {
    if (strDate == null || strDate.length() != 10) {
        throw new IllegalArgumentException(
                "引数の文字列["+ strDate +"]" +
                "は不正です。");
    }
    int yyyy = Integer.parseInt(strDate.substring(0,4));
    int MM = Integer.parseInt(strDate.substring(5,7));
    int dd = Integer.parseInt(strDate.substring(8,10));
    Calendar cal = Calendar.getInstance();
    cal.set(yyyy,MM-1,dd);
    int last = cal.getActualMaximum(Calendar.DATE);
    return last;
}


실행 결과

◆실행예

public static void main(String[] args) { System.out.println(getLastDay("2007/01/01")); System.out.println(getLastDay("2007/02/01")); System.out.println(getLastDay("2008)

◆출력 결과

31
28
29

※2008년은 윤년 때문에 2월의 월말 일자는 29일이 됩니다.

  • URL을(를) 확인했습니다!
목차