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


Java TimeZone inDaylightTime()用法及代碼示例


Java中的TimeZone類的inDaylightTime(Date)方法用於檢查和驗證給定的日期是否在夏時製中。

用法:

public abstract boolean 
    inDaylightTime(Date date)

參數:該方法采用Date類型的一個參數date,這是要驗證的給定日期。


返回值:如果傳遞的日期相對於夏時製無效,則該方法返回布爾值True,否則返回布爾值False。

以下程序說明了Java中inDaylightTime()方法的用法:
示例1:

// Java code to demonstrate 
// inDaylightTime() method 
  
import java.util.*; 
  
public class SimpleTimeZone_Demo { 
    public static void main(String[] args) 
    { 
        // Creating a TimeZone 
        TimeZone offtime_zone 
            = TimeZone.getTimeZone("Europe/Rome"); 
  
        // Creating date object 
        Date dt = new Date(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = offtime_zone.inDaylightTime(dt); 
  
        // Checking the value of day light 
        System.out.println("Is it in day"
                           + " light saving time? "
                           + bool_daylight); 
    } 
}
輸出:
Is it in day light saving time? true

示例2:

// Java code to demonstrate 
// inDaylightTime() method 
  
import java.util.*; 
  
public class SimpleTimeZone_Demo { 
    public static void main(String[] args) 
    { 
        // Creating a TimeZone 
        TimeZone offtime_zone 
            = TimeZone.getTimeZone("Pacific/Pago_Pago"); 
  
        // Creating date object 
        Date dt = new Date(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = offtime_zone.inDaylightTime(dt); 
  
        // Checking the value of day light 
        System.out.println("Is it in day"
                           + " light saving time? "
                           + bool_daylight); 
    } 
}
輸出:
Is it in day light saving time? false

參考:https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#inDaylightTime(java.util.Date)



相關用法


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