Java Calendar Day absoluteDay(Calendar cal, boolean use1904windowing)

Here you can find the source of absoluteDay(Calendar cal, boolean use1904windowing)

Description

Given a Calendar, return the number of days since 1900/12/31.

License

Apache License

Parameter

Parameter Description
cal the Calendar

Return

days number of days since 1900/12/31

Declaration

protected static int absoluteDay(Calendar cal, boolean use1904windowing) 

Method Source Code


//package com.java2s;
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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
    /*  w  w  w .  j  av a  2s.  c  o  m*/
   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.
==================================================================== */

import java.util.Calendar;

public class Main {
    /**
     * Given a Calendar, return the number of days since 1900/12/31.
     *
     * @return days number of days since 1900/12/31
     * @param  cal the Calendar
     * @exception IllegalArgumentException if date is invalid
     */
    protected static int absoluteDay(Calendar cal, boolean use1904windowing) {
        //20110402, henrichen@zkoss.org: could be 1989/12/31
        //return cal.get(Calendar.DAY_OF_YEAR)
        //       + daysInPriorYears(cal.get(Calendar.YEAR), use1904windowing);
        final int minY = use1904windowing ? 1903 : 1899;
        final int year = cal.get(Calendar.YEAR);
        return year == minY ? 0
                : cal.get(Calendar.DAY_OF_YEAR) + daysInPriorYears(cal.get(Calendar.YEAR), use1904windowing);
    }

    /**
     * Return the number of days in prior years since 1900
     *
     * @return    days  number of days in years prior to yr.
     * @param     yr    a year (1899 <= yr < 4000)
     * @param use1904windowing
     * @exception IllegalArgumentException if year is outside of range.
     */

    private static int daysInPriorYears(int yr, boolean use1904windowing) {
        if ((!use1904windowing && yr < 1900) || (use1904windowing && yr < 1900)) {
            throw new IllegalArgumentException("'year' must be 1900 or greater");
        }
        int yr1 = yr - 1;
        int leapDays = yr1 / 4 // plus julian leap days in prior years
                - yr1 / 100 // minus prior century years
                + yr1 / 400 // plus years divisible by 400
                - 460; // leap days in previous 1900 years

        return 365 * (yr - (use1904windowing ? 1904 : 1900)) + leapDays;
    }
}

Related

  1. absoluteDay(Calendar cal)
  2. afterCurrentDay(Calendar time)
  3. calendarAtStartOfToday()
  4. calendarDayToOrdinalDay(int dow)
  5. calToBeginningOfDay(Calendar c)