Java Day Of Year dayOfYear(int month, int day, int year)

Here you can find the source of dayOfYear(int month, int day, int year)

Description

return the day of year for the month and day, for the given year

License

Open Source License

Parameter

Parameter Description
month the month, january=1, february=2, etc.
day day of month
year four-digit year

Return

the day of year

Declaration

public static int dayOfYear(int month, int day, int year) 

Method Source Code

//package com.java2s;
/*/*from w  w  w.  j  a v a  2  s  .  c  o  m*/
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

public class Main {
    private final static int[][] dayOffset = { { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
            { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } };

    /**
     * return the day of year for the month and day, for the given year
     * @param month the month, january=1, february=2, etc.
     * @param day day of month
     * @param year four-digit year
     * @return the day of year
     */
    public static int dayOfYear(int month, int day, int year) {
        return day + dayOffset[isLeapYear(year) ? 1 : 0][month];
    }

    /**
     * return the leap year for years 1581-8999.
     * @param year the four-digit year.
     * @return true if the year is a leap year.
     */
    public static boolean isLeapYear(int year) {
        if (year < 1000)
            throw new IllegalArgumentException("year must be four-digits");
        return (year % 4) == 0 && (year % 100 != 0 || year % 400 == 0);
    }
}

Related

  1. currentYearMonthDay()
  2. dateOfYearBetween(final int month, final int dayOfMonth, final int fromMonth, final int fromDayOfMonth, final int toMonth, final int toDayOfMonth)
  3. dayOfYear(final int year, final int month, final int day)
  4. dayOfYear(int year, int month, int date)
  5. dayOfYear(int year, int month, int day)
  6. dayOfYear(int year, int month, int day)
  7. daysInPriorYears(final int y)