LocalDateTime类的toLocalDate()方法用于获取此LocalDateTime的LocalDate表示形式。此方法派生自Object Class,其行为类似。
用法:
public LocalDate toLocalDate()
参数:此方法不带参数。
返回值:此方法返回LocalDate值,它是此LocalDateTime的LocalDate表示形式。
以下示例程序旨在说明LocalDateTime.toLocalDate()方法:
示例1:
// Program to illustrate the toLocalDate() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Get the LocalDateTime instance
LocalDateTime dt = LocalDateTime.now();
// Get the LocalDate representation of this LocalDateTime
// using toLocalDate() method
System.out.println(dt.toLocalDate());
}
}
输出:
2018-11-30
示例2:
// Program to illustrate the toLocalDate() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Get the LocalDateTime instance
LocalDateTime dt
= LocalDateTime
.parse("2018-11-03T12:45:30");
// Get the LocalDate representation of this LocalDateTime
// using toLocalDate() method
System.out.println(dt.toLocalDate());
}
}
输出:
2018-11-03