Java程序将LocalDateTime转换为LocalDate和LocalTime
首先设置一个LocalDateTime:
LocalDate date = LocalDate.now();LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
现在,将LocalDateTime转换为LocalDate和LocalTime:
LocalDate localDate = LocalDateTime.now().toLocalDate();LocalTime localTime = LocalDateTime.now().toLocalTime();
示例
import java.time.LocalDate;import java.time.LocalDateTime;
import java.time.LocalTime;
public class Demo {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
System.out.println("DateTime = "+dateTime);
LocalDate localDate = LocalDateTime.now().toLocalDate();
LocalTime localTime = LocalDateTime.now().toLocalTime();
System.out.println("Date = "+localDate);
System.out.println("Time = "+localTime);
}
}
输出结果
DateTime = 2019-04-18T13:57:22.591327100Date = 2019-04-18
Time = 13:57:22.591327100
以上是 Java程序将LocalDateTime转换为LocalDate和LocalTime 的全部内容, 来源链接: utcz.com/z/362285.html