java怎么獲取當(dāng)月每天的日期號(hào)

小億
152
2024-07-27 01:55:10

在Java中,可以使用java.util.Calendar類(lèi)來(lái)獲取當(dāng)月每天的日期號(hào)。以下是一個(gè)示例代碼:

import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);

        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (int day = 1; day <= daysInMonth; day++) {
            calendar.set(Calendar.DAY_OF_MONTH, day);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            System.out.println(dayOfMonth);
        }
    }
}

這段代碼會(huì)輸出當(dāng)月的每一天的日期號(hào)。

0