LocalDateTime 输出年月日
时间: 2024-01-10 07:20:54 浏览: 74
以下是使用LocalDateTime输出年月日的示例代码:
```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);
}
}
```
输出结果为:
```
年月日: 2022-01-01
```
相关问题
LocalDateTime获取年月日
要获取LocalDateTime的年月日,可以使用format方法和DateTimeFormatter类来进行格式化。具体的代码如下所示:
```
LocalDateTime localDateTime = LocalDateTime.now();
String formattedDate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("当前日期为:" + formattedDate);
```
这段代码将会输出当前的年月日,格式为"yyyy-MM-dd",例如"2022-01-01"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [LocalDateTime只获取年月日](https://blog.csdn.net/sinat_29774479/article/details/81483741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
LocalDateTime取年月日
以下是使用LocalDateTime获取年月日的方法:
```java
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
int year = localDateTime.getYear();
int month = localDateTime.getMonthValue();
int day = localDateTime.getDayOfMonth();
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Day: " + day);
}
}
```
输出结果:
```
Year: 2021
Month: 10
Day: 20
```
阅读全文