Java Day in Month getDayOfMonth(int dayOfYear, boolean leap)

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

Description

Converts the number of elapsed days from beginning of the year to the date in the current 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 date in the current month (1-origin)

Declaration

public static int getDayOfMonth(int dayOfYear, boolean leap) 

Method Source Code

//package com.java2s;
/**//  w  w  w  . j a v a2s  . c  om
 * 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 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. getDayByOfSomeMonth(String curday, int month)
  2. getDayMonth(String dayofmonth)
  3. getDayOfMonth(Date d)
  4. getDayOfMonth(Date date)
  5. getDayOfMonth(Date p_date)
  6. getDayOfMonth(int month, int year)
  7. getDayOfMonth(long time)
  8. getDayOfMonth(Object millis)
  9. GetDayOfMonth(String aDatetime)