Java TimeZone observesDaylightTime()方法及示例

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]
结果是: 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()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程