Java中的 ChronoLocalDateTime toLocalDate() 方法及示例
ChronoLocalDateTime接口的toLocalDate()方法用于将此 ChronoLocalDateTime 转换为 LocalDate。该方法返回此 ChronoLocalDateTime 的 LocalDate 部分。
句法:
default LocalDate toLocalDate()
参数:此方法不接受任何参数。
返回值:此方法返回LocalDate ,即此 ChronoLocalDateTime 的 LocalDate
下面的程序说明了 toLocalDate() 方法:
方案一:
// Java program to demonstrate
// ChronoLocalDateTime.toLocalDate() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime
.parse("2019-12-31T19:15:30");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// print result
System.out.println("LocalDate: "
+ time.toLocalDate());
}
}
输出:
ChronoLocalDateTime: 2019-12-31T19:15:30
LocalDate: 2019-12-31
方案二:
// Java program to demonstrate
// ChronoLocalDateTime.toLocalDate() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoLocalDateTime object
ChronoLocalDateTime time
= LocalDateTime.parse(
"2018-10-25T23:12:31.123");
// print ChronoLocalDateTime
System.out.println("ChronoLocalDateTime: "
+ time);
// print result
System.out.println("LocalDate: "
+ time.toLocalDate());
}
}
输出:
ChronoLocalDateTime: 2018-10-25T23:12:31.123
LocalDate: 2018-10-25
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoLocalDateTime.html#toLocalDate–