loacalDateTime设置成年月日
时间: 2023-08-12 20:04:38 浏览: 32
你可以使用`LocalDateTime`类的`format`方法来日期的格式为年月日。以下是一个示例代码:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = now.format(formatter);
System.out.println(formattedDate);
}
}
```
在上述代码中,我们使用`DateTimeFormatter`类创建了一个自定义的日期格式模式"yyyy-MM-dd",它代表年-月-日的形式。然后,我们使用`format`方法将当前的`LocalDateTime`对象按照指定的格式转换为字符串,并打印输出。你可以根据需要调整格式模式来满足你的需求。