Java Calendar Day setDayToCalendar(int days, Calendar calendar)

Here you can find the source of setDayToCalendar(int days, Calendar calendar)

Description

Converts the number of elapsed days from 0001/01/01 (YYYY/MM/DD) to the corresponded date, and set it to the target calendar object.

License

Apache License

Parameter

Parameter Description
days the number of elapsed days from 0001/01/01 (YYYY/MM/DD)
calendar the target calendar object

Declaration

public static void setDayToCalendar(int days, Calendar calendar) 

Method Source Code

//package com.java2s;
/**// w  w  w  .j  a  va 2s . co  m
 * Copyright 2011-2017 Asakusa Framework Team.
 *
 * 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 {
    private static final int DAYS_YEAR = 365;
    private static final int DAYS_JANUARY = 31;
    private static final int DAYS_FEBRUARY = DAYS_JANUARY + 28;
    private static final int DAYS_MARCH = DAYS_FEBRUARY + 31;
    private static final int DAYS_APRIL = DAYS_MARCH + 30;
    private static final int DAYS_MAY = DAYS_APRIL + 31;
    private static final int DAYS_JUNE = DAYS_MAY + 30;
    private static final int DAYS_JULY = DAYS_JUNE + 31;
    private static final int DAYS_AUGUST = DAYS_JULY + 31;
    private static final int DAYS_SEPTEMBER = DAYS_AUGUST + 30;
    private static final int DAYS_OCTOBER = DAYS_SEPTEMBER + 31;
    private static final int DAYS_NOVEMBER = DAYS_OCTOBER + 30;
    private static final int YEARS_LEAP_CYCLE = 400;
    private static final int DAYS_LEAP_CYCLE = DAYS_YEAR * YEARS_LEAP_CYCLE + (YEARS_LEAP_CYCLE / 4)
            - (YEARS_LEAP_CYCLE / 100) + (YEARS_LEAP_CYCLE / 400);
    private static final int YEARS_CENTURY = 100;
    private static final int DAYS_CENTURY = DAYS_YEAR * YEARS_CENTURY + (YEARS_CENTURY / 4) - (YEARS_CENTURY / 100)
            + (YEARS_CENTURY / 400);
    private static final int YEARS_LEAP = 4;
    private static final int DAYS_LEAP = DAYS_YEAR * YEARS_LEAP + (YEARS_LEAP / 4) - (YEARS_LEAP / 100)
            + (YEARS_LEAP / 400);

    /**
     * Converts the number of elapsed days from {@code 0001/01/01 (YYYY/MM/DD)} to the corresponded date,
     * and set it to the target calendar object.
     * Note that, the hour, minute, second and millisecond fields will be set to {@code 0}.
     * @param days the number of elapsed days from {@code 0001/01/01 (YYYY/MM/DD)}
     * @param calendar the target calendar object
     * @since 0.2.2
     */
    public static void setDayToCalendar(int days, Calendar calendar) {
        int year = getYearFromDay(days);
        int daysInYear = days - getDayFromYear(year);
        boolean leap = isLeap(year);
        int month = getMonthOfYear(daysInYear, leap);
        int day = getDayOfMonth(daysInYear, leap);
        calendar.set(year, month - 1, day, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * Converts the number of elapsed days from {@code 0001/01/01 (YYYY/MM/DD)} to the corresponding year.
     * @param days the number of elapsed days
     * @return the corresponded year
     */
    public static int getYearFromDay(int days) {

        // the number of leap year cycles (400years)
        int cycles = days / DAYS_LEAP_CYCLE;
        int cycleRest = days % DAYS_LEAP_CYCLE;

        // the century offset in the current leap year cycle (0-3)
        int centInCycle = cycleRest / DAYS_CENTURY;
        int centRest = cycleRest % DAYS_CENTURY;
        centRest += DAYS_CENTURY * (centInCycle / (YEARS_LEAP_CYCLE / YEARS_CENTURY));
        centInCycle -= (centInCycle / (YEARS_LEAP_CYCLE / YEARS_CENTURY));

        // the leap year offset in the current century (0-24)
        int leapInCent = centRest / DAYS_LEAP;
        int leapRest = centRest % DAYS_LEAP;

        // the year offset since the last leap year (0-3)
        int yearInLeap = leapRest / DAYS_YEAR;
        yearInLeap -= (yearInLeap / YEARS_LEAP);

        // compute the year
        int year = YEARS_LEAP_CYCLE * cycles + YEARS_CENTURY * centInCycle + YEARS_LEAP * leapInCent + yearInLeap
                + 1;

        return year;
    }

    /**
     * Converts the year to the number of elapsed days from {@code 0001/01/01 (YYYY/MM/DD)} about
     * the first day of the specified year.
     * For example, {@code getDayFromYear(1)} returns just {@code 0}.
     * @param year the target year
     * @return the number of elapsed days about the first day of the specified year
     */
    public static int getDayFromYear(int year) {
        int y = year - 1;
        return DAYS_YEAR * y + (y / 4) - (y / 100) + (y / 400);
    }

    /**
     * Whether the target year is leap year or not.
     * @param year the target year
     * @return {@code true} if the target year is leap year, otherwise {@code false}
     */
    public static boolean isLeap(int year) {
        if (year % 4 != 0) {
            return false;
        }
        return (year % 100) != 0 || (year % 400) == 0;
    }

    /**
     * Converts the number of elapsed days from beginning of the year to the corresponding month.
     * @param dayOfYear the number of elapsed days from beginning of the year
     * @param leap whether the target year is leap year or not
     * @return the month including the target day (<em>1-origin</em>)
     */
    public static int getMonthOfYear(int dayOfYear, boolean leap) {
        int d = dayOfYear;
        if (d < DAYS_JANUARY) {
            return 1;
        }
        if (leap) {
            d--;
        }
        if (d < DAYS_FEBRUARY) {
            return 2;
        }
        if (d < DAYS_MARCH) {
            return 3;
        }
        if (d < DAYS_APRIL) {
            return 4;
        }
        if (d < DAYS_MAY) {
            return 5;
        }
        if (d < DAYS_JUNE) {
            return 6;
        }
        if (d < DAYS_JULY) {
            return 7;
        }
        if (d < DAYS_AUGUST) {
            return 8;
        }
        if (d < DAYS_SEPTEMBER) {
            return 9;
        }
        if (d < DAYS_OCTOBER) {
            return 10;
        }
        if (d < DAYS_NOVEMBER) {
            return 11;
        }
        return 12;
    }

    /**
     * Converts the number of elapsed days from beginning of the year to the date in the current month.
     * @param dayOfYear the number of elapsed days from beginning of the year
     * @param leap whether the target year is leap year or not
     * @return the date in the current month (1-origin)
     */
    public static int getDayOfMonth(int dayOfYear, boolean leap) {
        int d = dayOfYear;
        if (d < DAYS_JANUARY) {
            return d + 1;
        }
        if (d < DAYS_FEBRUARY) {
            return d - (DAYS_JANUARY - 1);
        }
        if (leap) {
            if (d == DAYS_FEBRUARY) {
                return 29;
            }
            d--;
        }
        if (d < DAYS_MARCH) {
            return d - (DAYS_FEBRUARY - 1);
        }
        if (d < DAYS_APRIL) {
            return d - (DAYS_MARCH - 1);
        }
        if (d < DAYS_MAY) {
            return d - (DAYS_APRIL - 1);
        }
        if (d < DAYS_JUNE) {
            return d - (DAYS_MAY - 1);
        }
        if (d < DAYS_JULY) {
            return d - (DAYS_JUNE - 1);
        }
        if (d < DAYS_AUGUST) {
            return d - (DAYS_JULY - 1);
        }
        if (d < DAYS_SEPTEMBER) {
            return d - (DAYS_AUGUST - 1);
        }
        if (d < DAYS_OCTOBER) {
            return d - (DAYS_SEPTEMBER - 1);
        }
        if (d < DAYS_NOVEMBER) {
            return d - (DAYS_OCTOBER - 1);
        }
        return d - (DAYS_NOVEMBER - 1);
    }
}

Related

  1. newCalendarForDay(Date date)
  2. resetToBeginningOfDay(Calendar c)
  3. sameDay(Calendar a, Calendar b)
  4. sameDay(Calendar c1, Calendar c2)
  5. sameDay(Calendar one, Calendar two)
  6. setEndDay(Calendar cal)
  7. setStartOfDay(final Calendar calendar)
  8. setStartTimeOfDay(Calendar calender)
  9. setToBeginningOfDay(Calendar cal)