Java 获取本年所有月份字符串时间集合 (yyyyMM 格式)
您可以使用java.time.YearMonth类来获取本年的所有月份的字符串时间集合。\n\n以下是一个示例代码:\n\njava\nimport java.time.YearMonth;\nimport java.time.format.DateTimeFormatter;\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Main {\n public static void main(String[] args) {\n int year = YearMonth.now().getYear();\n List<String> months = new ArrayList<>();\n \n for (int month = 1; month <= 12; month++) {\n YearMonth yearMonth = YearMonth.of(year, month);\n String formattedMonth = yearMonth.format(DateTimeFormatter.ofPattern("yyyyMM"));\n months.add(formattedMonth);\n }\n \n System.out.println(months);\n }\n}\n\n\n此代码将输出一个包含本年所有月份的字符串时间集合,时间格式为"yyyyMM",例如:\n\n\n[202101, 202102, 202103, 202104, 202105, 202106, 202107, 202108, 202109, 202110, 202111, 202112]\n
原文地址: https://www.cveoy.top/t/topic/ojC0 著作权归作者所有。请勿转载和采集!