Java Calendar Day absoluteDay(Calendar cal)

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

Description

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

License

Apache License

Parameter

Parameter Description
cal the Calendar

Return

days number of days since 1600/12/31

Declaration


private static int absoluteDay(Calendar cal) 

Method Source Code


//package com.java2s;
/* ====================================================================
   Copyright 2002-2004   Apache Software Foundation
    /* w w w  .j  a  va  2s.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.
==================================================================== */

import java.util.Calendar;

public class Main {
    /**
     * Given a Calendar, return the number of days since 1600/12/31.
     *
     * @return days number of days since 1600/12/31
     * @param  cal the Calendar
     * @exception IllegalArgumentException if date is invalid
     */

    private static int absoluteDay(Calendar cal) {
        return cal.get(Calendar.DAY_OF_YEAR) + daysInPriorYears(cal.get(Calendar.YEAR));
    }

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

    private static int daysInPriorYears(int yr) {
        if (yr < 1601) {
            throw new IllegalArgumentException("'year' must be 1601 or greater");
        }
        int y = yr - 1601;
        int days = 365 * y // days in prior years
                + y / 4 // plus julian leap days in prior years
                - y / 100 // minus prior century years
                + y / 400; // plus years divisible by 400

        return days;
    }
}

Related

  1. absoluteDay(Calendar cal, boolean use1904windowing)
  2. afterCurrentDay(Calendar time)
  3. calendarAtStartOfToday()
  4. calendarDayToOrdinalDay(int dow)