溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java Calendar類獲取特定月份天數(shù)

發(fā)布時間:2024-08-10 13:41:25 來源:億速云 閱讀:80 作者:小樊 欄目:編程語言
import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        // 設(shè)置月份,注意月份從0開始計數(shù),即0代表1月,1代表2月,以此類推
        int month = 2; // 獲取3月份的天數(shù)
        calendar.set(Calendar.MONTH, month);

        // 獲取該月份的天數(shù)
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Number of days in the month: " + daysInMonth);
    }
}

在上面的示例代碼中,我們使用java.util.Calendar類來獲取特定月份的天數(shù)。首先,我們創(chuàng)建一個Calendar實(shí)例并設(shè)置要獲取天數(shù)的月份。然后,我們通過調(diào)用getActualMaximum(Calendar.DAY_OF_MONTH)方法來獲取該月份的天數(shù),并將結(jié)果打印輸出。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI