DateComponents.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.time;

  18. import java.io.Serializable;
  19. import java.text.DecimalFormat;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;

  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;

  24. /** Class representing a date broken up as year, month and day components.
  25.  * <p>This class uses the astronomical convention for calendars,
  26.  * which is also the convention used by <code>java.util.Date</code>:
  27.  * a year zero is present between years -1 and +1, and 10 days are
  28.  * missing in 1582. The calendar used around these special dates are:</p>
  29.  * <ul>
  30.  *   <li>up to 0000-12-31 : proleptic julian calendar</li>
  31.  *   <li>from 0001-01-01 to 1582-10-04: julian calendar</li>
  32.  *   <li>from 1582-10-15: gregorian calendar</li>
  33.  * </ul>
  34.  * <p>Instances of this class are guaranteed to be immutable.</p>
  35.  * @see TimeComponents
  36.  * @see DateTimeComponents
  37.  * @author Luc Maisonobe
  38.  */
  39. public class DateComponents implements Serializable, Comparable<DateComponents> {

  40.     /** Reference epoch for julian dates: -4712-01-01.
  41.      * <p>Both <code>java.util.Date</code> and {@link DateComponents} classes
  42.      * follow the astronomical conventions and consider a year 0 between
  43.      * years -1 and +1, hence this reference date lies in year -4712 and not
  44.      * in year -4713 as can be seen in other documents or programs that obey
  45.      * a different convention (for example the <code>convcal</code> utility).</p>
  46.      */
  47.     public static final DateComponents JULIAN_EPOCH;

  48.     /** Reference epoch for modified julian dates: 1858-11-17. */
  49.     public static final DateComponents MODIFIED_JULIAN_EPOCH;

  50.     /** Reference epoch for 1950 dates: 1950-01-01. */
  51.     public static final DateComponents FIFTIES_EPOCH;

  52.     /** Reference epoch for CCSDS Time Code Format (CCSDS 301.0-B-4): 1958-01-01. */
  53.     public static final DateComponents CCSDS_EPOCH;

  54.     /** Reference epoch for Galileo System Time: 1999-08-22. */
  55.     public static final DateComponents GALILEO_EPOCH;

  56.     /** Reference epoch for GPS weeks: 1980-01-06. */
  57.     public static final DateComponents GPS_EPOCH;

  58.     /** J2000.0 Reference epoch: 2000-01-01. */
  59.     public static final DateComponents J2000_EPOCH;

  60.     /** Java Reference epoch: 1970-01-01. */
  61.     public static final DateComponents JAVA_EPOCH;

  62.     /** Serializable UID. */
  63.     private static final long serialVersionUID = -2462694707837970938L;

  64.     /** Factory for proleptic julian calendar (up to 0000-12-31). */
  65.     private static final YearFactory PROLEPTIC_JULIAN_FACTORY = new ProlepticJulianFactory();

  66.     /** Factory for julian calendar (from 0001-01-01 to 1582-10-04). */
  67.     private static final YearFactory JULIAN_FACTORY           = new JulianFactory();

  68.     /** Factory for gregorian calendar (from 1582-10-15). */
  69.     private static final YearFactory GREGORIAN_FACTORY        = new GregorianFactory();

  70.     /** Factory for leap years. */
  71.     private static final MonthDayFactory LEAP_YEAR_FACTORY    = new LeapYearFactory();

  72.     /** Factory for non-leap years. */
  73.     private static final MonthDayFactory COMMON_YEAR_FACTORY  = new CommonYearFactory();

  74.     /** Format for years. */
  75.     private static final DecimalFormat FOUR_DIGITS = new DecimalFormat("0000");

  76.     /** Format for months and days. */
  77.     private static final DecimalFormat TWO_DIGITS  = new DecimalFormat("00");

  78.     /** Offset between J2000 epoch and modified julian day epoch. */
  79.     private static final int MJD_TO_J2000 = 51544;

  80.     /** Basic and extended format calendar date. */
  81.     private static Pattern CALENDAR_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?(\\d\\d)-?(\\d\\d)$");

  82.     /** Basic and extended format ordinal date. */
  83.     private static Pattern ORDINAL_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?(\\d\\d\\d)$");

  84.     /** Basic and extended format week date. */
  85.     private static Pattern WEEK_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?W(\\d\\d)-?(\\d)$");

  86.     static {
  87.         // this static statement makes sure the reference epoch are initialized
  88.         // once AFTER the various factories have been set up
  89.         JULIAN_EPOCH          = new DateComponents(-4712,  1,  1);
  90.         MODIFIED_JULIAN_EPOCH = new DateComponents(1858, 11, 17);
  91.         FIFTIES_EPOCH         = new DateComponents(1950, 1, 1);
  92.         CCSDS_EPOCH           = new DateComponents(1958, 1, 1);
  93.         GALILEO_EPOCH         = new DateComponents(1999, 8, 22);
  94.         GPS_EPOCH             = new DateComponents(1980, 1, 6);
  95.         J2000_EPOCH           = new DateComponents(2000, 1, 1);
  96.         JAVA_EPOCH            = new DateComponents(1970, 1, 1);
  97.     }

  98.     /** Year number. */
  99.     private final int year;

  100.     /** Month number. */
  101.     private final int month;

  102.     /** Day number. */
  103.     private final int day;

  104.     /** Build a date from its components.
  105.      * @param year year number (may be 0 or negative for BC years)
  106.      * @param month month number from 1 to 12
  107.      * @param day day number from 1 to 31
  108.      * @exception IllegalArgumentException if inconsistent arguments
  109.      * are given (parameters out of range, february 29 for non-leap years,
  110.      * dates during the gregorian leap in 1582 ...)
  111.      */
  112.     public DateComponents(final int year, final int month, final int day)
  113.         throws IllegalArgumentException {

  114.         // very rough range check
  115.         // (just to avoid ArrayOutOfboundException in MonthDayFactory later)
  116.         if ((month < 1) || (month > 12)) {
  117.             throw OrekitException.createIllegalArgumentException(OrekitMessages.NON_EXISTENT_MONTH,
  118.                                                                  month);
  119.         }

  120.         // start by trusting the parameters
  121.         this.year  = year;
  122.         this.month = month;
  123.         this.day   = day;

  124.         // build a check date from the J2000 day
  125.         final DateComponents check = new DateComponents(getJ2000Day());

  126.         // check the parameters for mismatch
  127.         // (i.e. invalid date components, like 29 february on non-leap years)
  128.         if ((year != check.year) || (month != check.month) || (day != check.day)) {
  129.             throw OrekitException.createIllegalArgumentException(OrekitMessages.NON_EXISTENT_YEAR_MONTH_DAY,
  130.                                                                  year, month, day);
  131.         }

  132.     }

  133.     /** Build a date from its components.
  134.      * @param year year number (may be 0 or negative for BC years)
  135.      * @param month month enumerate
  136.      * @param day day number from 1 to 31
  137.      * @exception IllegalArgumentException if inconsistent arguments
  138.      * are given (parameters out of range, february 29 for non-leap years,
  139.      * dates during the gregorian leap in 1582 ...)
  140.      */
  141.     public DateComponents(final int year, final Month month, final int day)
  142.         throws IllegalArgumentException {
  143.         this(year, month.getNumber(), day);
  144.     }

  145.     /** Build a date from a year and day number.
  146.      * @param year year number (may be 0 or negative for BC years)
  147.      * @param dayNumber day number in the year from 1 to 366
  148.      * @exception IllegalArgumentException if dayNumber is out of range
  149.      * with respect to year
  150.      */
  151.     public DateComponents(final int year, final int dayNumber)
  152.         throws IllegalArgumentException {
  153.         this(J2000_EPOCH, new DateComponents(year - 1, 12, 31).getJ2000Day() + dayNumber);
  154.         if (dayNumber != getDayOfYear()) {
  155.             throw OrekitException.createIllegalArgumentException(OrekitMessages.NON_EXISTENT_DAY_NUMBER_IN_YEAR,
  156.                                                                  dayNumber, year);
  157.         }
  158.     }

  159.     /** Build a date from its offset with respect to a {@link #J2000_EPOCH}.
  160.      * @param offset offset with respect to a {@link #J2000_EPOCH}
  161.      * @see #getJ2000Day()
  162.      */
  163.     public DateComponents(final int offset) {

  164.         // we follow the astronomical convention for calendars:
  165.         // we consider a year zero and 10 days are missing in 1582
  166.         // from 1582-10-15: gregorian calendar
  167.         // from 0001-01-01 to 1582-10-04: julian calendar
  168.         // up to 0000-12-31 : proleptic julian calendar
  169.         YearFactory yFactory = GREGORIAN_FACTORY;
  170.         if (offset < -152384) {
  171.             if (offset > -730122) {
  172.                 yFactory = JULIAN_FACTORY;
  173.             } else {
  174.                 yFactory = PROLEPTIC_JULIAN_FACTORY;
  175.             }
  176.         }
  177.         year = yFactory.getYear(offset);
  178.         final int dayInYear = offset - yFactory.getLastJ2000DayOfYear(year - 1);

  179.         // handle month/day according to the year being a common or leap year
  180.         final MonthDayFactory mdFactory =
  181.             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
  182.         month = mdFactory.getMonth(dayInYear);
  183.         day   = mdFactory.getDay(dayInYear, month);

  184.     }

  185.     /** Build a date from its offset with respect to a reference epoch.
  186.      * <p>This constructor is mainly useful to build a date from a modified
  187.      * julian day (using {@link #MODIFIED_JULIAN_EPOCH}) or a GPS week number
  188.      * (using {@link #GPS_EPOCH}).</p>
  189.      * @param epoch reference epoch
  190.      * @param offset offset with respect to a reference epoch
  191.      * @see #DateComponents(int)
  192.      * @see #getMJD()
  193.      */
  194.     public DateComponents(final DateComponents epoch, final int offset) {
  195.         this(epoch.getJ2000Day() + offset);
  196.     }

  197.     /** Build a date from week components.
  198.      * <p>The calendar week number is a number between 1 and 52 or 53 depending
  199.      * on the year. Week 1 is defined by ISO as the one that includes the first
  200.      * Thursday of a year. Week 1 may therefore start the previous year and week
  201.      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
  202.      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
  203.      * is in fact the first day of year 1995). This date would beAnother example is calendar date
  204.      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
  205.      * first week of 1997 is in fact the last day of year 1996).</p>
  206.      * @param wYear year associated to week numbering
  207.      * @param week week number in year,from 1 to 52 or 53
  208.      * @param dayOfWeek day of week, from 1 (Monday) to 7 (Sunday)
  209.      * @return a builded date
  210.      * @exception IllegalArgumentException if inconsistent arguments
  211.      * are given (parameters out of range, week 53 on a 52 weeks year ...)
  212.      */
  213.     public static DateComponents createFromWeekComponents(final int wYear, final int week, final int dayOfWeek)
  214.         throws IllegalArgumentException {

  215.         final DateComponents firstWeekMonday = new DateComponents(getFirstWeekMonday(wYear));
  216.         final DateComponents d = new DateComponents(firstWeekMonday, 7 * week + dayOfWeek - 8);

  217.         // check the parameters for invalid date components
  218.         if ((week != d.getCalendarWeek()) || (dayOfWeek != d.getDayOfWeek())) {
  219.             throw OrekitException.createIllegalArgumentException(OrekitMessages.NON_EXISTENT_WEEK_DATE,
  220.                                                                  wYear, week, dayOfWeek);
  221.         }

  222.         return d;

  223.     }

  224.     /** Parse a string in ISO-8601 format to build a date.
  225.      * <p>The supported formats are:
  226.      * <ul>
  227.      *   <li>basic format calendar date: YYYYMMDD</li>
  228.      *   <li>extended format calendar date: YYYY-MM-DD</li>
  229.      *   <li>basic format ordinal date: YYYYDDD</li>
  230.      *   <li>extended format ordinal date: YYYY-DDD</li>
  231.      *   <li>basic format week date: YYYYWwwD</li>
  232.      *   <li>extended format week date: YYYY-Www-D</li>
  233.      * </ul>
  234.      * As shown by the list above, only the complete representations defined in section 4.1
  235.      * of ISO-8601 standard are supported, neither expended representations nor representations
  236.      * with reduced accuracy are supported.
  237.      * </p>
  238.      * <p>
  239.      * Parsing a single integer as a julian day is <em>not</em> supported as it may be ambiguous
  240.      * with either the basic format calendar date or the basic format ordinal date depending
  241.      * on the number of digits.
  242.      * </p>
  243.      * @param string string to parse
  244.      * @return a parsed date
  245.      * @exception IllegalArgumentException if string cannot be parsed
  246.      */
  247.     public static  DateComponents parseDate(final String string) {

  248.         // is the date a calendar date ?
  249.         final Matcher calendarMatcher = CALENDAR_FORMAT.matcher(string);
  250.         if (calendarMatcher.matches()) {
  251.             return new DateComponents(Integer.parseInt(calendarMatcher.group(1)),
  252.                                       Integer.parseInt(calendarMatcher.group(2)),
  253.                                       Integer.parseInt(calendarMatcher.group(3)));
  254.         }

  255.         // is the date an ordinal date ?
  256.         final Matcher ordinalMatcher = ORDINAL_FORMAT.matcher(string);
  257.         if (ordinalMatcher.matches()) {
  258.             return new DateComponents(Integer.parseInt(ordinalMatcher.group(1)),
  259.                                       Integer.parseInt(ordinalMatcher.group(2)));
  260.         }

  261.         // is the date a week date ?
  262.         final Matcher weekMatcher = WEEK_FORMAT.matcher(string);
  263.         if (weekMatcher.matches()) {
  264.             return createFromWeekComponents(Integer.parseInt(weekMatcher.group(1)),
  265.                                             Integer.parseInt(weekMatcher.group(2)),
  266.                                             Integer.parseInt(weekMatcher.group(3)));
  267.         }

  268.         throw OrekitException.createIllegalArgumentException(OrekitMessages.NON_EXISTENT_DATE, string);

  269.     }

  270.     /** Get the year number.
  271.      * @return year number (may be 0 or negative for BC years)
  272.      */
  273.     public int getYear() {
  274.         return year;
  275.     }

  276.     /** Get the month.
  277.      * @return month number from 1 to 12
  278.      */
  279.     public int getMonth() {
  280.         return month;
  281.     }

  282.     /** Get the month as an enumerate.
  283.      * @return month as an enumerate
  284.      */
  285.     public Month getMonthEnum() {
  286.         return Month.getMonth(month);
  287.     }

  288.     /** Get the day.
  289.      * @return day number from 1 to 31
  290.      */
  291.     public int getDay() {
  292.         return day;
  293.     }

  294.     /** Get the day number with respect to J2000 epoch.
  295.      * @return day number with respect to J2000 epoch
  296.      */
  297.     public int getJ2000Day() {
  298.         YearFactory yFactory = GREGORIAN_FACTORY;
  299.         if (year < 1583) {
  300.             if (year < 1) {
  301.                 yFactory = PROLEPTIC_JULIAN_FACTORY;
  302.             } else if ((year < 1582) || (month < 10) || ((month < 11) && (day < 5))) {
  303.                 yFactory = JULIAN_FACTORY;
  304.             }
  305.         }
  306.         final MonthDayFactory mdFactory =
  307.             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
  308.         return yFactory.getLastJ2000DayOfYear(year - 1) +
  309.                mdFactory.getDayInYear(month, day);
  310.     }

  311.     /** Get the modified julian day.
  312.      * @return modified julian day
  313.      */
  314.     public int getMJD() {
  315.         return MJD_TO_J2000 + getJ2000Day();
  316.     }

  317.     /** Get the calendar week number.
  318.      * <p>The calendar week number is a number between 1 and 52 or 53 depending
  319.      * on the year. Week 1 is defined by ISO as the one that includes the first
  320.      * Thursday of a year. Week 1 may therefore start the previous year and week
  321.      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
  322.      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
  323.      * is in fact the first day of year 1995). Another example is calendar date
  324.      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
  325.      * first week of 1997 is in fact the last day of year 1996).</p>
  326.      * @return calendar week number
  327.      */
  328.     public int getCalendarWeek() {
  329.         final int firstWeekMonday = getFirstWeekMonday(year);
  330.         int daysSincefirstMonday = getJ2000Day() - firstWeekMonday;
  331.         if (daysSincefirstMonday < 0) {
  332.             // we are still in a week from previous year
  333.             daysSincefirstMonday += firstWeekMonday - getFirstWeekMonday(year - 1);
  334.         } else if (daysSincefirstMonday > 363) {
  335.             // up to three days at end of year may belong to first week of next year
  336.             // (by chance, there is no need for a specific check in year 1582 ...)
  337.             final int weekYearLength = getFirstWeekMonday(year + 1) - firstWeekMonday;
  338.             if (daysSincefirstMonday >= weekYearLength) {
  339.                 daysSincefirstMonday -= weekYearLength;
  340.             }
  341.         }
  342.         return 1 + daysSincefirstMonday / 7;
  343.     }

  344.     /** Get the monday of a year first week.
  345.      * @param year year to consider
  346.      * @return day of the monday of the first weak of year
  347.      */
  348.     private static int getFirstWeekMonday(final int year) {
  349.         final int yearFirst = new DateComponents(year, 1, 1).getJ2000Day();
  350.         final int offsetToMonday = 4 - (yearFirst + 2) % 7;
  351.         return yearFirst + offsetToMonday + ((offsetToMonday > 3) ? -7 : 0);
  352.     }

  353.     /** Get the day of week.
  354.      * <p>Day of week is a number between 1 (Monday) and 7 (Sunday).</p>
  355.      * @return day of week
  356.      */
  357.     public int getDayOfWeek() {
  358.         final int dow = (getJ2000Day() + 6) % 7; // result is between -6 and +6
  359.         return (dow < 1) ? (dow + 7) : dow;
  360.     }

  361.     /** Get the day number in year.
  362.      * <p>Day number in year is between 1 (January 1st) and either 365 or
  363.      * 366 inclusive depending on year.</p>
  364.      * @return day number in year
  365.      */
  366.     public int getDayOfYear() {
  367.         return getJ2000Day() - new DateComponents(year - 1, 12, 31).getJ2000Day();
  368.     }

  369.     /** Get a string representation (ISO-8601) of the date.
  370.      * @return string representation of the date.
  371.      */
  372.     public String toString() {
  373.         return new StringBuffer().
  374.                append(FOUR_DIGITS.format(year)).append('-').
  375.                append(TWO_DIGITS.format(month)).append('-').
  376.                append(TWO_DIGITS.format(day)).
  377.                toString();
  378.     }

  379.     /** {@inheritDoc} */
  380.     public int compareTo(final DateComponents other) {
  381.         final int j2000Day = getJ2000Day();
  382.         final int otherJ2000Day = other.getJ2000Day();
  383.         if (j2000Day < otherJ2000Day) {
  384.             return -1;
  385.         } else if (j2000Day > otherJ2000Day) {
  386.             return 1;
  387.         }
  388.         return 0;
  389.     }

  390.     /** {@inheritDoc} */
  391.     public boolean equals(final Object other) {
  392.         try {
  393.             final DateComponents otherDate = (DateComponents) other;
  394.             return (otherDate != null) && (year == otherDate.year) &&
  395.                    (month == otherDate.month) && (day == otherDate.day);
  396.         } catch (ClassCastException cce) {
  397.             return false;
  398.         }
  399.     }

  400.     /** {@inheritDoc} */
  401.     public int hashCode() {
  402.         return (year << 16) ^ (month << 8) ^ day;
  403.     }

  404.     /** Interface for dealing with years sequences according to some calendar. */
  405.     private interface YearFactory {

  406.         /** Get the year number for a given day number with respect to J2000 epoch.
  407.          * @param j2000Day day number with respect to J2000 epoch
  408.          * @return year number
  409.          */
  410.         int getYear(int j2000Day);

  411.         /** Get the day number with respect to J2000 epoch for new year's Eve.
  412.          * @param year year number
  413.          * @return day number with respect to J2000 epoch for new year's Eve
  414.          */
  415.         int getLastJ2000DayOfYear(int year);

  416.         /** Check if a year is a leap or common year.
  417.          * @param year year number
  418.          * @return true if year is a leap year
  419.          */
  420.         boolean isLeap(int year);

  421.     }

  422.     /** Class providing a years sequence compliant with the proleptic julian calendar. */
  423.     private static class ProlepticJulianFactory implements YearFactory {

  424.         /** {@inheritDoc} */
  425.         public int getYear(final int j2000Day) {
  426.             return  -((-4 * j2000Day - 2920488) / 1461);
  427.         }

  428.         /** {@inheritDoc} */
  429.         public int getLastJ2000DayOfYear(final int year) {
  430.             return (1461 * year + 1) / 4 - 730123;
  431.         }

  432.         /** {@inheritDoc} */
  433.         public boolean isLeap(final int year) {
  434.             return (year % 4) == 0;
  435.         }

  436.     }

  437.     /** Class providing a years sequence compliant with the julian calendar. */
  438.     private static class JulianFactory implements YearFactory {

  439.         /** {@inheritDoc} */
  440.         public int getYear(final int j2000Day) {
  441.             return  (4 * j2000Day + 2921948) / 1461;
  442.         }

  443.         /** {@inheritDoc} */
  444.         public int getLastJ2000DayOfYear(final int year) {
  445.             return (1461 * year) / 4 - 730122;
  446.         }

  447.         /** {@inheritDoc} */
  448.         public boolean isLeap(final int year) {
  449.             return (year % 4) == 0;
  450.         }

  451.     }

  452.     /** Class providing a years sequence compliant with the gregorian calendar. */
  453.     private static class GregorianFactory implements YearFactory {

  454.         /** {@inheritDoc} */
  455.         public int getYear(final int j2000Day) {

  456.             // year estimate
  457.             int year = (400 * j2000Day + 292194288) / 146097;

  458.             // the previous estimate is one unit too high in some rare cases
  459.             // (240 days in the 400 years gregorian cycle, about 0.16%)
  460.             if (j2000Day <= getLastJ2000DayOfYear(year - 1)) {
  461.                 --year;
  462.             }

  463.             // exact year
  464.             return year;

  465.         }

  466.         /** {@inheritDoc} */
  467.         public int getLastJ2000DayOfYear(final int year) {
  468.             return (1461 * year) / 4 - year / 100 + year / 400 - 730120;
  469.         }

  470.         /** {@inheritDoc} */
  471.         public boolean isLeap(final int year) {
  472.             return ((year % 4) == 0) && (((year % 400) == 0) || ((year % 100) != 0));
  473.         }

  474.     }

  475.     /** Interface for dealing with months sequences according to leap/common years. */
  476.     private interface MonthDayFactory {

  477.         /** Get the month number for a given day number within year.
  478.          * @param dayInYear day number within year
  479.          * @return month number
  480.          */
  481.         int getMonth(int dayInYear);

  482.         /** Get the day number for given month and day number within year.
  483.          * @param dayInYear day number within year
  484.          * @param month month number
  485.          * @return day number
  486.          */
  487.         int getDay(int dayInYear, int month);

  488.         /** Get the day number within year for given month and day numbers.
  489.          * @param month month number
  490.          * @param day day number
  491.          * @return day number within year
  492.          */
  493.         int getDayInYear(int month, int day);

  494.     }

  495.     /** Class providing the months sequence for leap years. */
  496.     private static class LeapYearFactory implements MonthDayFactory {

  497.         /** Months succession definition. */
  498.         private static final int[] PREVIOUS_MONTH_END_DAY = {
  499.             0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
  500.         };

  501.         /** {@inheritDoc} */
  502.         public int getMonth(final int dayInYear) {
  503.             return (dayInYear < 32) ? 1 : (10 * dayInYear + 313) / 306;
  504.         }

  505.         /** {@inheritDoc} */
  506.         public int getDay(final int dayInYear, final int month) {
  507.             return dayInYear - PREVIOUS_MONTH_END_DAY[month];
  508.         }

  509.         /** {@inheritDoc} */
  510.         public int getDayInYear(final int month, final int day) {
  511.             return day + PREVIOUS_MONTH_END_DAY[month];
  512.         }

  513.     }

  514.     /** Class providing the months sequence for common years. */
  515.     private static class CommonYearFactory implements MonthDayFactory {

  516.         /** Months succession definition. */
  517.         private static final int[] PREVIOUS_MONTH_END_DAY = {
  518.             0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  519.         };

  520.         /** {@inheritDoc} */
  521.         public int getMonth(final int dayInYear) {
  522.             return (dayInYear < 32) ? 1 : (10 * dayInYear + 323) / 306;
  523.         }

  524.         /** {@inheritDoc} */
  525.         public int getDay(final int dayInYear, final int month) {
  526.             return dayInYear - PREVIOUS_MONTH_END_DAY[month];
  527.         }

  528.         /** {@inheritDoc} */
  529.         public int getDayInYear(final int month, final int day) {
  530.             return day + PREVIOUS_MONTH_END_DAY[month];
  531.         }

  532.     }

  533. }