📜  Java中的 TimeZone 观察DaylightTime() 方法及示例

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

Java中的 TimeZone 观察DaylightTime() 方法及示例

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);
    }
}
输出:

示例 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);
    }
}
输出:

参考: https: Java/util/TimeZone.html#observesDaylightTime()