Java Calendar Hour hoursInDay(Calendar cal)

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

Description

This function returns number of hour in a day when given a Calendar with appropriate TZ.

License

Apache License

Parameter

Parameter Description

Return

number of hour in that day.

Declaration

public static int hoursInDay(Calendar cal) 

Method Source Code

//package com.java2s;
/**//from   ww w .  j  a  v  a  2 s .  co  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 {
    /**
     * 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. getHour(Calendar cal)
  2. getHour(Calendar cal)
  3. getHour(Calendar calendar)
  4. hour(Calendar calendar)
  5. hourFloor(Calendar calendar)
  6. moveHoursHand(int aNumberOfHours, Calendar aCalendar)
  7. moveToCalendarHourJust(Calendar cal)
  8. moveToCalendarHourTerminal(Calendar cal)
  9. sameHour(Calendar one, Calendar two)