Java Leap Year Check getMonthOfYear(int dayOfYear, boolean leap)

Here you can find the source of getMonthOfYear(int dayOfYear, boolean leap)

Description

Converts the number of elapsed days from beginning of the year to the corresponding month.

License

Apache License

Parameter

Parameter Description
dayOfYear the number of elapsed days from beginning of the year
leap whether the target year is leap year or not

Return

the month including the target day (1-origin)

Declaration

public static int getMonthOfYear(int dayOfYear, boolean leap) 

Method Source Code

//package com.java2s;
/**//w  w w.  j av a 2s . com
 * 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_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;

    /**
     * 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;
    }
}

Related

  1. getLeapYear(int theyear)
  2. isGregorianLeapYear(int gregorianYear)
  3. isGregorianLeapYear(int gregorianYear)
  4. isJalaliLeapYear(int year)
  5. isJulianLeapYear(int normalizedJulianYear)