Java 中的 ChronoLocalDateTime toLocalTime()方法,示例

原文:https://www . geeksforgeeks . org/chronolocaldatetime-to localtime-method-in-Java-with-examples/

计时本地日期时间接口的 toLocalTime() 方法用于将该计时本地日期时间转换为本地时间。该方法返回该 ChronoLocalDateTime 的 LocalTime 部分。

语法:

default LocalTime toLocalTime()

参数:此方法不接受任何参数。

返回值:该方法返回本地时间,即该时间本地日期时间的本地时间

下面的程序说明了 toLocalTime()方法:

程序 1:

// Java program to demonstrate
// ChronoLocalDateTime.toLocalTime() 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("LocalTime: "
                           + time.toLocalTime());
    }
}

输出:

ChronoLocalDateTime: 2019-12-31T19:15:30
LocalTime: 19:15:30

程序二:

// Java program to demonstrate
// ChronoLocalDateTime.toLocalTime() 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("LocalTime: "
                           + time.toLocalTime());
    }
}

输出:

ChronoLocalDateTime: 2018-10-25T23:12:31.123
LocalTime: 23:12:31.123

参考:https://docs . Oracle . com/javae/9/docs/API/Java/time/cron/chronical datetime . html # tolocaltime】