Java ChronoZonedDateTime toLocalDateTime()方法及示例
ChronoZonedDateTime 接口的 toLocalDateTime() 方法用于将该ChronoZonedDateTime转换成LocalDateTime。
语法
default LocalDateTime toLocalDateTime()
参数: 该方法不接受任何参数。
返回值: 该方法返回 LocalDateTime ,它与ChronoZonedDateTime所代表的LocalDateTime相同。
以下程序说明了toLocalDateTime()方法。
程序1 :
// Java program to demonstrate
// ChronoZonedDateTime.toLocalDateTime() method
import java.time.*;
import java.time.chrono.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoZonedDateTime object
ChronoZonedDateTime time
= ZonedDateTime
.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// print ChronoZonedDateTime
System.out.println("ChronoZonedDateTime: "
+ time);
// print result
System.out.println("LocalDateTime: "
+ time.toLocalDateTime());
}
}
输出:
ChronoZonedDateTime: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
LocalDateTime: 2018-12-06T19:21:12.123
程序2
// Java program to demonstrate
// ChronoZonedDateTime.toLocalDateTime() method
import java.time.*;
import java.time.chrono.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create ChronoZonedDateTime object
ChronoZonedDateTime time
= ZonedDateTime.parse(
"1918-10-25T23:12:38.543+02:00[Europe/Paris]");
// print ChronoZonedDateTime
System.out.println("ChronoZonedDateTime: "
+ time);
// print result
System.out.println("LocalDateTime: "
+ time.toLocalDateTime());
}
}
输出:
ChronoZonedDateTime: 1918-10-25T23:12:38.543Z[Europe/Paris]
LocalDateTime: 1918-10-25T23:12:38.543
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoZonedDateTime.html#toLocalDateTime-