📌  相关文章
📜  Java中的 ChronoLocalDateTime toLocalTime() 方法及示例

📅  最后修改于: 2022-05-13 01:55:25.694000             🧑  作者: Mango

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

ChronoLocalDateTime接口的toLocalTime()方法用于将此 ChronoLocalDateTime 转换为 LocalTime。该方法返回此 ChronoLocalDateTime 的 LocalTime 部分。

句法:

default LocalTime toLocalTime()

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

返回值:此方法返回LocalTime ,即此 ChronoLocalDateTime 的 LocalTime

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

方案一:

// 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/javase/9/docs/api/ Java/time/chrono/ChronoLocalDateTime.html#toLocalTime–