📜  Java中的 TimeZone inDaylightTime() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.209000             🧑  作者: Mango

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: Java Java.util.Date)