java8 获取某年的第一月
要获取某年的第一月,可以使用java.time.Year类和java.time.Month枚举类。具体步骤如下:
- 使用
Year.of()方法创建一个Year对象,表示指定的年份。 - 使用
Year.getMonth()方法获取该年份的第一个月份。 - 使用
Month.getValue()方法获取该月份的数值(1表示一月,2表示二月,依此类推)。
下面是一个示例代码:
import java.time.Month;
import java.time.Year;
public class Main {
public static void main(String[] args) {
int year = 2022; // 要获取的年份
Year y = Year.of(year);
Month firstMonth = y.getMonth();
int firstMonthValue = firstMonth.getValue();
System.out.println("The first month of " + year + " is " + firstMonthValue);
}
}
输出结果为:
The first month of 2022 is 1
这表示2022年的第一个月是一月
原文地址: https://www.cveoy.top/t/topic/it2l 著作权归作者所有。请勿转载和采集!