We would like to know how to check day light saving type for given date.
Checks if the type of the given date. Possible return values are standard time, the date when to switch to daylight saving time (in Europe the last Sunday in March), daylight saving time or the date when to switch back to standard time (in Europe the last Sunday in October).
/*from w ww . j a v a 2 s . c o m*/ /** * This class provides some useful utility methods for date and time operations. * The implementations is done using the new date and time api in Java8. * * @see add further links * * @author created: 7droids.org on 27.02.2014 20:45:58 * @author last change: $Author: $ on $Date: $ * @version $Revision: $ */ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] argv) { System.out.println(getDSTType(LocalDate.now())); } enum DayType { STANDARD_TIME, DAYLIGHT_SAVING_TIME, TO_DAYLIGHT_SAVING_TIME, TO_STANDARD_TIME }; /** * Checks if the type of the given date. Possible return values are standard * time, the date when to switch to daylight saving time (in Europe the last * Sunday in March), daylight saving time or the date when to switch back to * standard time (in Europe the last Sunday in October). * * @return DayType * @param cal * Date to check, cannot be null */ public static DayType getDSTType(LocalDate cal) { DayType status = DayType.DAYLIGHT_SAVING_TIME; LocalDateTime testDate = cal.atStartOfDay(); ZonedDateTime zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault()); // Find type of day if (zdt.getZone().getRules() .isDaylightSavings(testDate.toInstant(zdt.getOffset()))) status = DayType.DAYLIGHT_SAVING_TIME; else status = DayType.STANDARD_TIME; // Check the day after testDate = testDate.plusDays(1); zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault()); // Find type of day after if (zdt.getZone().getRules() .isDaylightSavings(testDate.toInstant(zdt.getOffset()))) { if (status != DayType.DAYLIGHT_SAVING_TIME) status = DayType.TO_DAYLIGHT_SAVING_TIME; } else { if (status == DayType.DAYLIGHT_SAVING_TIME) status = DayType.TO_STANDARD_TIME; } return status; } }
The code above generates the following result.