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


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


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

用法:

public 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 time zone object 
        SimpleTimeZone simtimeobj 
            = new SimpleTimeZone(540, "GMT"); 
  
        // Creating date object 
        Date dt = new Date(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = simtimeobj.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

示例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 
        SimpleTimeZone simtimeobj 
            = new SimpleTimeZone(400, "AUS"); 
  
        // Creating a Calendar object 
        Calendar c1 = Calendar.getInstance(); 
  
        // Set Month 
        // MONTH starts with 0 i.e.(0 - Jan) 
        c1.set(Calendar.MONTH, 00); 
  
        // Set Date 
        c1.set(Calendar.DATE, 30); 
  
        // Set Year 
        c1.set(Calendar.YEAR, 2019); 
  
        // Creating a date object 
        // with specified time. 
        Date dt = c1.getTime(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = simtimeobj.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/8/docs/api/java/util/SimpleTimeZone.html#inDaylightTime-java.util.Date-



相關用法


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