Here you can find the source of getYearFromDay(int days)
Parameter | Description |
---|---|
days | the number of elapsed days |
public static int getYearFromDay(int days)
//package com.java2s; /**// w ww . j ava 2 s. c o 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. */ public class Main { private static final int DAYS_YEAR = 365; 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 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; } }