当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SimpleTimeZone inDaylightTime()用法及代码示例


Java中SimpleTimeZone类的inDaylightTime(Date)方法用于检查和验证给定日期是否在夏时制中。

用法:

public 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 time zone object 
        SimpleTimeZone simtimeobj 
            = new SimpleTimeZone(540, "GMT"); 
  
        // Creating date object 
        Date dt = new Date(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = simtimeobj.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

示例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 
        SimpleTimeZone simtimeobj 
            = new SimpleTimeZone(400, "AUS"); 
  
        // Creating a Calendar object 
        Calendar c1 = Calendar.getInstance(); 
  
        // Set Month 
        // MONTH starts with 0 i.e.(0 - Jan) 
        c1.set(Calendar.MONTH, 00); 
  
        // Set Date 
        c1.set(Calendar.DATE, 30); 
  
        // Set Year 
        c1.set(Calendar.YEAR, 2019); 
  
        // Creating a date object 
        // with specified time. 
        Date dt = c1.getTime(); 
  
        // Verifying daylight 
        boolean bool_daylight 
            = simtimeobj.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/8/docs/api/java/util/SimpleTimeZone.html#inDaylightTime-java.util.Date-



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 SimpleTimeZone inDaylightTime() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。