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


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


Java中的TimeZone類的observesDaylightTime()方法用於檢查和驗證給定日期是否在夏時製中,或者將來任何時候都將發生從標準時間到夏時製的任何轉換。

用法:

public boolean observesDaylightTime()

參數:該方法不帶任何參數。


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

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

// Java code to demonstrate 
// observesDaylightTime() method 
  
import java.util.*; 
  
public class TimeZone_Demo { 
    public static void main(String[] args) 
    { 
        // Creating a time zone object 
        TimeZone timeobj 
            = TimeZone 
                  .getTimeZone("Pacific/Pago_Pago"); 
  
        // Displaying the time zone 
  
        System.out.println(timeobj); 
        // Verifying daylight 
        boolean bool_daylight 
            = timeobj.observesDaylightTime(); 
  
        // Checking the value of day light 
        System.out.println("The result is: "
                           + bool_daylight); 
    } 
}
輸出:

sun.util.calendar.ZoneInfo[id=”Pacific/Pago_Pago”, offset=-39600000, dstSavings=0,
useDaylight=false, transitions=3, lastRule=null]
The result is: false

示例2:

// Java code to demonstrate 
// inDaylightTime() method 
  
import java.util.*; 
  
public class SimpleTimeZone_Demo { 
    public static void main(String[] args) 
    { 
        // Creating a time zone object 
        TimeZone timeobj 
            = TimeZone 
                  .getTimeZone("Indian/Chagos"); 
  
        // Displaying the time zone 
  
        System.out.println(timeobj); 
        // Verifying daylight 
        boolean bool_daylight 
            = timeobj.observesDaylightTime(); 
  
        // Checking the value of day light 
        System.out.println("The result is: "
                           + bool_daylight); 
    } 
}
輸出:

sun.util.calendar.ZoneInfo[id=”Indian/Chagos”, offset=21600000, dstSavings=0, useDaylight=false, transitions=4, lastRule=null]
The result is: false

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



相關用法


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