Java 获取当前月份之前的所有月份(整数集合)
您可以使用java.time.LocalDate类来获取当前月份之前的所有月份,并将它们存储在一个Integer类型的集合中。以下是一个示例代码:
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();
        List<Integer> months = new ArrayList<>();
        for (Month month : Month.values()) {
            if (month.getValue() <= currentMonth.getValue()) {
                months.add(month.getValue());
            }
        }
        for (int year = 2010; year <= currentYear; year++) {
            for (int month = 1; month <= 12; month++) {
                if (year < currentYear || month <= currentMonth.getValue()) {
                    months.add(month);
                }
            }
        }
        System.out.println(months);
    }
}
此代码将打印出从2010年1月到当前年份和月份之间的所有月份的整数集合。
原文地址: https://www.cveoy.top/t/topic/lx5P 著作权归作者所有。请勿转载和采集!