2024-07-24
一言
你必须要变强,才可以守护身边的人! --- 《坎公骑冠剑》 · 飞小RAN
YearMonth and LocalDate
在Java中,可以使用YearMonth
类将其转换为LocalDate
。YearMonth
表示特定年份和月份的组合,而LocalDate
则表示具体的日期,包括年、月和日。因此,在转换时,需要指定一个具体的日。例如,可以选择月份的第一天或者最后一天。
下面是一些示例代码,展示了如何将YearMonth
转换为LocalDate
:
将YearMonth转换为某个月的第一天
import java.time.YearMonth;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
YearMonth yearMonth = YearMonth.of(2023, 7); // 2023年7月
LocalDate firstDayOfMonth = yearMonth.atDay(1); // 该月的第一天
System.out.println("YearMonth: " + yearMonth);
System.out.println("First day of month: " + firstDayOfMonth);
}
}