温馨提示×

java如何获取当前年月日

小亿
109
2023-08-08 01:24:16
栏目: 编程语言

在Java中,可以使用java.time包中的LocalDate类来获取当前年月日。以下是一个示例代码:

import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();
System.out.println("当前年月日:" + year + "-" + month + "-" + day);
}
}

运行以上代码,将输出当前年月日。

0