當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ChronoLocalDateTime toLocalTime()用法及代碼示例


ChronoLocalDateTime接口的toLocalTime()方法用於將此ChronoLocalDateTime轉換為LocalTime。該方法返回此ChronoLocalDateTime的LocalTime部分。

用法:

default LocalTime toLocalTime()

參數:此方法不接受任何參數。


返回值:此方法返回LocalTime,它是此ChronoLocalDateTime的LocalTime。

以下示例程序旨在說明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

示例2:

// 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–



相關用法


注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoLocalDateTime toLocalTime() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。