Here you can find the source of dayOfYear(int month, int day, int year)
Parameter | Description |
---|---|
month | the month, january=1, february=2, etc. |
day | day of month |
year | four-digit year |
public static int dayOfYear(int month, int day, int year)
//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); } }