Java TimeZone inDaylightTime()方法及示例

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://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#inDaylightTime(java.util.Date)。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程