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–
相關用法
- Java OffsetDateTime getHour()用法及代碼示例
- Java OffsetTime getHour()用法及代碼示例
- Java LocalTime getHour()用法及代碼示例
- Java ZonedDateTime getHour()用法及代碼示例
- Java LocalDateTime until()用法及代碼示例
- Java LocalDateTime plus()用法及代碼示例
- Java LocalDateTime with()用法及代碼示例
- Java LocalDateTime from()用法及代碼示例
- Java LocalDateTime now()用法及代碼示例
- Java LocalDateTime plusWeeks()用法及代碼示例
- Java LocalDateTime plusHours()用法及代碼示例
- Java LocalDateTime plusMinutes()用法及代碼示例
- Java LocalDateTime isEqual()用法及代碼示例
- Java LocalDateTime parse()用法及代碼示例
- Java LocalDateTime plusDays()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalDateTime getHour() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。