Java Calendar Day isDSTChangeDay(Calendar cal)

Here you can find the source of isDSTChangeDay(Calendar cal)

Description

Determine whether a specific date is on DST change day

License

Apache License

Parameter

Parameter Description

Return

true , if it DST change date otherwise false

Declaration

public static boolean isDSTChangeDay(Calendar cal) 

Method Source Code

//package com.java2s;
/**/*  w  w  w  .j av a  2 s  . c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. See accompanying LICENSE file.
 */

import java.util.Calendar;

import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    /**
     * Determine whether a specific date is on DST change day
     *
     * @param cal: Date to know if it is DST change day. Appropriate TZ is specified
     * @return true , if it DST change date otherwise false
     */
    public static boolean isDSTChangeDay(Calendar cal) {
        return hoursInDay(cal) != 24;
    }

    /**
     * This function returns number of hour in a day when given a Calendar with appropriate TZ. It consider DST to find
     * the number of hours. Generally it is 24. At some tZ, in one day of a year it is 23 and another day it is 25
     *
     * @param cal: The date for which the number of hours is requested
     * @return number of hour in that day.
     */
    public static int hoursInDay(Calendar cal) {
        Calendar localCal = new GregorianCalendar(cal.getTimeZone());
        localCal.set(Calendar.MILLISECOND, 0);
        localCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 30, 0);
        localCal.add(Calendar.HOUR_OF_DAY, 24);
        switch (localCal.get(Calendar.HOUR_OF_DAY)) {
        case 1:
            return 23;
        case 23:
            return 25;
        default: // Case 0
            return 24;
        }
    }

    public static TimeZone getTimeZone(String tzId) {
        if (tzId == null) {
            throw new IllegalArgumentException("Invalid TimeZone: " + tzId);
        }
        TimeZone tz = TimeZone.getTimeZone(tzId);
        if (!tz.getID().equals(tzId)) {
            throw new IllegalArgumentException("Invalid TimeZone: " + tzId);
        }
        return tz;
    }
}

Related

  1. incrementOneDay(Calendar calendar)
  2. isAfterDay(Calendar cal1, Calendar cal2)
  3. isAllDayEvent(Calendar dtStart, Calendar dtEnd)
  4. isBusinessDay(Calendar cal)
  5. isDayAfter(Calendar calendar, Calendar baseCalendar)
  6. isEndOfDay(Calendar calendar)
  7. isHolyday(Calendar c)
  8. isRestDay(Calendar cal)
  9. isSameDay(Calendar c1, Calendar c2)