Java Utility Library

Java TimeZone - inDaylightTime() Method



The java.util.TimeZone.inDaylightTime() method is used to check if the given date is in Daylight Saving Time in this time zone.

Syntax

public abstract boolean inDaylightTime(Date date)

Parameters

date Specify the given Date.

Return Value

Returns true if the given date is in Daylight Saving Time, false, otherwise.

Exception

NA

Example:

The example below explains how to use java.util.TimeZone.inDaylightTime() method.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a TimeZone object
    TimeZone tz = TimeZone.getTimeZone("CST");
   
    //creating date objects
    GregorianCalendar cal1 = new GregorianCalendar(2018, 05, 05);
    GregorianCalendar cal2 = new GregorianCalendar(2018, 12, 05);
    Date dt1 = cal1.getTime();
    Date dt2 = cal2.getTime();

    //checking if dt1 is in daylight saving
    System.out.print("dt1 is in daylight saving: ");
    System.out.println(tz.inDaylightTime(dt1));

    //checking if dt2 is in daylight saving
    System.out.print("dt2 is in daylight saving: ");
    System.out.println(tz.inDaylightTime(dt2));
  }
}

The output of the above code will be:

dt1 is in daylight saving: true
dt2 is in daylight saving: false

❮ Java.util - TimeZone