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)。