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