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


Java LocalDateTime getHour()用法及代碼示例


LocalDateTime類的getHour()方法用於返回hour-of-day字段。此方法返回一個介於0到23之間的整數值,即一天的小時數。

用法:

public int getHour()

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


返回值:此方法返回代表hour-of-day的整數值,範圍為0到23。

以下示例程序旨在說明LocalDateTime.getHour()方法:

示例1:

// Java program to demonstrate 
// LocalDateTime.getHour() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalDateTime Object 
        LocalDateTime local 
            = LocalDateTime.parse("2018-12-03T12:39:10"); 
  
        // get HourOfDay 
        int hourOfDay = local.getHour(); 
  
        // print result 
        System.out.println("HourOfDay: "
                           + hourOfDay); 
    } 
}
輸出:
HourOfDay: 12

示例2:

// Java program to demonstrate 
// LocalDateTime.getHour() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalDateTime Object 
        LocalDateTime local 
            = LocalDateTime.parse("2019-01-28T22:29:10"); 
  
        // get HourOfDay 
        int hourOfDay = local.getHour(); 
  
        // print result 
        System.out.println("HourOfDay: "
                           + hourOfDay); 
    } 
}
輸出:
HourOfDay: 22

參考:https://docs.oracle.com/javase/9​​/docs/api/java/time/LocalDateTime.html#getHour–



相關用法


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