LocalDateTime 为年月日 加上 8:10 代码
时间: 2023-10-31 22:59:44 浏览: 46
你可以使用`LocalDateTime`的`plus`方法来实现年月日加上8:10的操作。以下是示例代码:
```java
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
LocalDateTime newDateTime = dateTime.plusHours(8).plusMinutes(10);
System.out.println("原始日期时间:" + dateTime);
System.out.println("加上8小时10分钟后的日期时间:" + newDateTime);
}
}
```
这个代码会打印出当前的日期时间以及加上8小时10分钟后的日期时间。注意,这个例子中使用了`now`方法获取当前的`LocalDateTime`,你可以根据你的需求替换为指定的日期时间对象。
相关问题
LocalDateTime 为年月日 加上 hh:mm 动态的
你可以使用DateTimeFormatter类中的ofPattern方法来格式化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 HH:mm");
String formattedDateTime = now.format(formatter);
System.out.println("Formatted DateTime: " + formattedDateTime);
}
}
```
这将输出当前的年月日和小时分钟,例如:`Formatted DateTime: 2022-01-01 12:34`。你可以根据需要调整日期时间格式的模式字符串来满足你的特定需求。
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
```