DateComponents.java

  1. /* Copyright 2002-2018 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.OrekitIllegalArgumentException;
  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.     /** Maximum supported date.
  63.      * <p>
  64.      * This is date 5881610-07-11 which corresponds to {@code Integer.MAX_VALUE}
  65.      * days after {@link #J2000_EPOCH}.
  66.      * </p>
  67.      * @since 9.0
  68.      */
  69.     public static final DateComponents MAX_EPOCH;

  70.     /** Maximum supported date.
  71.      * <p>
  72.      * This is date -5877490-03-03, which corresponds to {@code Integer.MIN_VALUE}
  73.      * days before {@link #J2000_EPOCH}.
  74.      * </p>
  75.      * @since 9.0
  76.      */
  77.     public static final DateComponents MIN_EPOCH;

  78.     /** Serializable UID. */
  79.     private static final long serialVersionUID = -2462694707837970938L;

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

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

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

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

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

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

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

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

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

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

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

  102.     static {
  103.         // this static statement makes sure the reference epoch are initialized
  104.         // once AFTER the various factories have been set up
  105.         JULIAN_EPOCH          = new DateComponents(-4712,  1,  1);
  106.         MODIFIED_JULIAN_EPOCH = new DateComponents(1858, 11, 17);
  107.         FIFTIES_EPOCH         = new DateComponents(1950, 1, 1);
  108.         CCSDS_EPOCH           = new DateComponents(1958, 1, 1);
  109.         GALILEO_EPOCH         = new DateComponents(1999, 8, 22);
  110.         GPS_EPOCH             = new DateComponents(1980, 1, 6);
  111.         J2000_EPOCH           = new DateComponents(2000, 1, 1);
  112.         JAVA_EPOCH            = new DateComponents(1970, 1, 1);
  113.         MAX_EPOCH             = new DateComponents(Integer.MAX_VALUE);
  114.         MIN_EPOCH             = new DateComponents(Integer.MIN_VALUE);
  115.     }

  116.     /** Year number. */
  117.     private final int year;

  118.     /** Month number. */
  119.     private final int month;

  120.     /** Day number. */
  121.     private final int day;

  122.     /** Build a date from its components.
  123.      * @param year year number (may be 0 or negative for BC years)
  124.      * @param month month number from 1 to 12
  125.      * @param day day number from 1 to 31
  126.      * @exception IllegalArgumentException if inconsistent arguments
  127.      * are given (parameters out of range, february 29 for non-leap years,
  128.      * dates during the gregorian leap in 1582 ...)
  129.      */
  130.     public DateComponents(final int year, final int month, final int day)
  131.         throws IllegalArgumentException {

  132.         // very rough range check
  133.         // (just to avoid ArrayOutOfboundException in MonthDayFactory later)
  134.         if ((month < 1) || (month > 12)) {
  135.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_MONTH, month);
  136.         }

  137.         // start by trusting the parameters
  138.         this.year  = year;
  139.         this.month = month;
  140.         this.day   = day;

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

  143.         // check the parameters for mismatch
  144.         // (i.e. invalid date components, like 29 february on non-leap years)
  145.         if ((year != check.year) || (month != check.month) || (day != check.day)) {
  146.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_YEAR_MONTH_DAY,
  147.                                                       year, month, day);
  148.         }

  149.     }

  150.     /** Build a date from its components.
  151.      * @param year year number (may be 0 or negative for BC years)
  152.      * @param month month enumerate
  153.      * @param day day number from 1 to 31
  154.      * @exception IllegalArgumentException if inconsistent arguments
  155.      * are given (parameters out of range, february 29 for non-leap years,
  156.      * dates during the gregorian leap in 1582 ...)
  157.      */
  158.     public DateComponents(final int year, final Month month, final int day)
  159.         throws IllegalArgumentException {
  160.         this(year, month.getNumber(), day);
  161.     }

  162.     /** Build a date from a year and day number.
  163.      * @param year year number (may be 0 or negative for BC years)
  164.      * @param dayNumber day number in the year from 1 to 366
  165.      * @exception IllegalArgumentException if dayNumber is out of range
  166.      * with respect to year
  167.      */
  168.     public DateComponents(final int year, final int dayNumber)
  169.         throws IllegalArgumentException {
  170.         this(J2000_EPOCH, new DateComponents(year - 1, 12, 31).getJ2000Day() + dayNumber);
  171.         if (dayNumber != getDayOfYear()) {
  172.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_DAY_NUMBER_IN_YEAR,
  173.                                                      dayNumber, year);
  174.         }
  175.     }

  176.     /** Build a date from its offset with respect to a {@link #J2000_EPOCH}.
  177.      * @param offset offset with respect to a {@link #J2000_EPOCH}
  178.      * @see #getJ2000Day()
  179.      */
  180.     public DateComponents(final int offset) {

  181.         // we follow the astronomical convention for calendars:
  182.         // we consider a year zero and 10 days are missing in 1582
  183.         // from 1582-10-15: gregorian calendar
  184.         // from 0001-01-01 to 1582-10-04: julian calendar
  185.         // up to 0000-12-31 : proleptic julian calendar
  186.         YearFactory yFactory = GREGORIAN_FACTORY;
  187.         if (offset < -152384) {
  188.             if (offset > -730122) {
  189.                 yFactory = JULIAN_FACTORY;
  190.             } else {
  191.                 yFactory = PROLEPTIC_JULIAN_FACTORY;
  192.             }
  193.         }
  194.         year = yFactory.getYear(offset);
  195.         final int dayInYear = offset - yFactory.getLastJ2000DayOfYear(year - 1);

  196.         // handle month/day according to the year being a common or leap year
  197.         final MonthDayFactory mdFactory =
  198.             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
  199.         month = mdFactory.getMonth(dayInYear);
  200.         day   = mdFactory.getDay(dayInYear, month);

  201.     }

  202.     /** Build a date from its offset with respect to a reference epoch.
  203.      * <p>This constructor is mainly useful to build a date from a modified
  204.      * julian day (using {@link #MODIFIED_JULIAN_EPOCH}) or a GPS week number
  205.      * (using {@link #GPS_EPOCH}).</p>
  206.      * @param epoch reference epoch
  207.      * @param offset offset with respect to a reference epoch
  208.      * @see #DateComponents(int)
  209.      * @see #getMJD()
  210.      */
  211.     public DateComponents(final DateComponents epoch, final int offset) {
  212.         this(epoch.getJ2000Day() + offset);
  213.     }

  214.     /** Build a date from week components.
  215.      * <p>The calendar week number is a number between 1 and 52 or 53 depending
  216.      * on the year. Week 1 is defined by ISO as the one that includes the first
  217.      * Thursday of a year. Week 1 may therefore start the previous year and week
  218.      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
  219.      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
  220.      * is in fact the first day of year 1995). This date would beAnother example is calendar date
  221.      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
  222.      * first week of 1997 is in fact the last day of year 1996).</p>
  223.      * @param wYear year associated to week numbering
  224.      * @param week week number in year, from 1 to 52 or 53
  225.      * @param dayOfWeek day of week, from 1 (Monday) to 7 (Sunday)
  226.      * @return a builded date
  227.      * @exception IllegalArgumentException if inconsistent arguments
  228.      * are given (parameters out of range, week 53 on a 52 weeks year ...)
  229.      */
  230.     public static DateComponents createFromWeekComponents(final int wYear, final int week, final int dayOfWeek)
  231.         throws IllegalArgumentException {

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

  234.         // check the parameters for invalid date components
  235.         if ((week != d.getCalendarWeek()) || (dayOfWeek != d.getDayOfWeek())) {
  236.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_WEEK_DATE,
  237.                                                      wYear, week, dayOfWeek);
  238.         }

  239.         return d;

  240.     }

  241.     /** Parse a string in ISO-8601 format to build a date.
  242.      * <p>The supported formats are:
  243.      * <ul>
  244.      *   <li>basic format calendar date: YYYYMMDD</li>
  245.      *   <li>extended format calendar date: YYYY-MM-DD</li>
  246.      *   <li>basic format ordinal date: YYYYDDD</li>
  247.      *   <li>extended format ordinal date: YYYY-DDD</li>
  248.      *   <li>basic format week date: YYYYWwwD</li>
  249.      *   <li>extended format week date: YYYY-Www-D</li>
  250.      * </ul>
  251.      *
  252.      * <p> As shown by the list above, only the complete representations defined in section 4.1
  253.      * of ISO-8601 standard are supported, neither expended representations nor representations
  254.      * with reduced accuracy are supported.
  255.      *
  256.      * <p>
  257.      * Parsing a single integer as a julian day is <em>not</em> supported as it may be ambiguous
  258.      * with either the basic format calendar date or the basic format ordinal date depending
  259.      * on the number of digits.
  260.      * </p>
  261.      * @param string string to parse
  262.      * @return a parsed date
  263.      * @exception IllegalArgumentException if string cannot be parsed
  264.      */
  265.     public static  DateComponents parseDate(final String string) {

  266.         // is the date a calendar date ?
  267.         final Matcher calendarMatcher = CALENDAR_FORMAT.matcher(string);
  268.         if (calendarMatcher.matches()) {
  269.             return new DateComponents(Integer.parseInt(calendarMatcher.group(1)),
  270.                                       Integer.parseInt(calendarMatcher.group(2)),
  271.                                       Integer.parseInt(calendarMatcher.group(3)));
  272.         }

  273.         // is the date an ordinal date ?
  274.         final Matcher ordinalMatcher = ORDINAL_FORMAT.matcher(string);
  275.         if (ordinalMatcher.matches()) {
  276.             return new DateComponents(Integer.parseInt(ordinalMatcher.group(1)),
  277.                                       Integer.parseInt(ordinalMatcher.group(2)));
  278.         }

  279.         // is the date a week date ?
  280.         final Matcher weekMatcher = WEEK_FORMAT.matcher(string);
  281.         if (weekMatcher.matches()) {
  282.             return createFromWeekComponents(Integer.parseInt(weekMatcher.group(1)),
  283.                                             Integer.parseInt(weekMatcher.group(2)),
  284.                                             Integer.parseInt(weekMatcher.group(3)));
  285.         }

  286.         throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_DATE, string);

  287.     }

  288.     /** Get the year number.
  289.      * @return year number (may be 0 or negative for BC years)
  290.      */
  291.     public int getYear() {
  292.         return year;
  293.     }

  294.     /** Get the month.
  295.      * @return month number from 1 to 12
  296.      */
  297.     public int getMonth() {
  298.         return month;
  299.     }

  300.     /** Get the month as an enumerate.
  301.      * @return month as an enumerate
  302.      */
  303.     public Month getMonthEnum() {
  304.         return Month.getMonth(month);
  305.     }

  306.     /** Get the day.
  307.      * @return day number from 1 to 31
  308.      */
  309.     public int getDay() {
  310.         return day;
  311.     }

  312.     /** Get the day number with respect to J2000 epoch.
  313.      * @return day number with respect to J2000 epoch
  314.      */
  315.     public int getJ2000Day() {
  316.         YearFactory yFactory = GREGORIAN_FACTORY;
  317.         if (year < 1583) {
  318.             if (year < 1) {
  319.                 yFactory = PROLEPTIC_JULIAN_FACTORY;
  320.             } else if ((year < 1582) || (month < 10) || ((month < 11) && (day < 5))) {
  321.                 yFactory = JULIAN_FACTORY;
  322.             }
  323.         }
  324.         final MonthDayFactory mdFactory =
  325.             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
  326.         return yFactory.getLastJ2000DayOfYear(year - 1) +
  327.                mdFactory.getDayInYear(month, day);
  328.     }

  329.     /** Get the modified julian day.
  330.      * @return modified julian day
  331.      */
  332.     public int getMJD() {
  333.         return MJD_TO_J2000 + getJ2000Day();
  334.     }

  335.     /** Get the calendar week number.
  336.      * <p>The calendar week number is a number between 1 and 52 or 53 depending
  337.      * on the year. Week 1 is defined by ISO as the one that includes the first
  338.      * Thursday of a year. Week 1 may therefore start the previous year and week
  339.      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
  340.      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
  341.      * is in fact the first day of year 1995). Another example is calendar date
  342.      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
  343.      * first week of 1997 is in fact the last day of year 1996).</p>
  344.      * @return calendar week number
  345.      */
  346.     public int getCalendarWeek() {
  347.         final int firstWeekMonday = getFirstWeekMonday(year);
  348.         int daysSincefirstMonday = getJ2000Day() - firstWeekMonday;
  349.         if (daysSincefirstMonday < 0) {
  350.             // we are still in a week from previous year
  351.             daysSincefirstMonday += firstWeekMonday - getFirstWeekMonday(year - 1);
  352.         } else if (daysSincefirstMonday > 363) {
  353.             // up to three days at end of year may belong to first week of next year
  354.             // (by chance, there is no need for a specific check in year 1582 ...)
  355.             final int weekYearLength = getFirstWeekMonday(year + 1) - firstWeekMonday;
  356.             if (daysSincefirstMonday >= weekYearLength) {
  357.                 daysSincefirstMonday -= weekYearLength;
  358.             }
  359.         }
  360.         return 1 + daysSincefirstMonday / 7;
  361.     }

  362.     /** Get the monday of a year first week.
  363.      * @param year year to consider
  364.      * @return day of the monday of the first weak of year
  365.      */
  366.     private static int getFirstWeekMonday(final int year) {
  367.         final int yearFirst = new DateComponents(year, 1, 1).getJ2000Day();
  368.         final int offsetToMonday = 4 - (yearFirst + 2) % 7;
  369.         return yearFirst + offsetToMonday + ((offsetToMonday > 3) ? -7 : 0);
  370.     }

  371.     /** Get the day of week.
  372.      * <p>Day of week is a number between 1 (Monday) and 7 (Sunday).</p>
  373.      * @return day of week
  374.      */
  375.     public int getDayOfWeek() {
  376.         final int dow = (getJ2000Day() + 6) % 7; // result is between -6 and +6
  377.         return (dow < 1) ? (dow + 7) : dow;
  378.     }

  379.     /** Get the day number in year.
  380.      * <p>Day number in year is between 1 (January 1st) and either 365 or
  381.      * 366 inclusive depending on year.</p>
  382.      * @return day number in year
  383.      */
  384.     public int getDayOfYear() {
  385.         return getJ2000Day() - new DateComponents(year - 1, 12, 31).getJ2000Day();
  386.     }

  387.     /** Get a string representation (ISO-8601) of the date.
  388.      * @return string representation of the date.
  389.      */
  390.     public String toString() {
  391.         return new StringBuffer().
  392.                append(FOUR_DIGITS.format(year)).append('-').
  393.                append(TWO_DIGITS.format(month)).append('-').
  394.                append(TWO_DIGITS.format(day)).
  395.                toString();
  396.     }

  397.     /** {@inheritDoc} */
  398.     public int compareTo(final DateComponents other) {
  399.         final int j2000Day = getJ2000Day();
  400.         final int otherJ2000Day = other.getJ2000Day();
  401.         if (j2000Day < otherJ2000Day) {
  402.             return -1;
  403.         } else if (j2000Day > otherJ2000Day) {
  404.             return 1;
  405.         }
  406.         return 0;
  407.     }

  408.     /** {@inheritDoc} */
  409.     public boolean equals(final Object other) {
  410.         try {
  411.             final DateComponents otherDate = (DateComponents) other;
  412.             return (otherDate != null) && (year == otherDate.year) &&
  413.                    (month == otherDate.month) && (day == otherDate.day);
  414.         } catch (ClassCastException cce) {
  415.             return false;
  416.         }
  417.     }

  418.     /** {@inheritDoc} */
  419.     public int hashCode() {
  420.         return (year << 16) ^ (month << 8) ^ day;
  421.     }

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

  424.         /** Get the year number for a given day number with respect to J2000 epoch.
  425.          * @param j2000Day day number with respect to J2000 epoch
  426.          * @return year number
  427.          */
  428.         int getYear(int j2000Day);

  429.         /** Get the day number with respect to J2000 epoch for new year's Eve.
  430.          * @param year year number
  431.          * @return day number with respect to J2000 epoch for new year's Eve
  432.          */
  433.         int getLastJ2000DayOfYear(int year);

  434.         /** Check if a year is a leap or common year.
  435.          * @param year year number
  436.          * @return true if year is a leap year
  437.          */
  438.         boolean isLeap(int year);

  439.     }

  440.     /** Class providing a years sequence compliant with the proleptic Julian calendar. */
  441.     private static class ProlepticJulianFactory implements YearFactory {

  442.         /** {@inheritDoc} */
  443.         public int getYear(final int j2000Day) {
  444.             return  (int) -((-4l * j2000Day - 2920488l) / 1461l);
  445.         }

  446.         /** {@inheritDoc} */
  447.         public int getLastJ2000DayOfYear(final int year) {
  448.             return 365 * year + (year + 1) / 4 - 730123;
  449.         }

  450.         /** {@inheritDoc} */
  451.         public boolean isLeap(final int year) {
  452.             return (year % 4) == 0;
  453.         }

  454.     }

  455.     /** Class providing a years sequence compliant with the Julian calendar. */
  456.     private static class JulianFactory implements YearFactory {

  457.         /** {@inheritDoc} */
  458.         public int getYear(final int j2000Day) {
  459.             return  (int) ((4l * j2000Day + 2921948l) / 1461l);
  460.         }

  461.         /** {@inheritDoc} */
  462.         public int getLastJ2000DayOfYear(final int year) {
  463.             return 365 * year + year / 4 - 730122;
  464.         }

  465.         /** {@inheritDoc} */
  466.         public boolean isLeap(final int year) {
  467.             return (year % 4) == 0;
  468.         }

  469.     }

  470.     /** Class providing a years sequence compliant with the Gregorian calendar. */
  471.     private static class GregorianFactory implements YearFactory {

  472.         /** {@inheritDoc} */
  473.         public int getYear(final int j2000Day) {

  474.             // year estimate
  475.             int year = (int) ((400l * j2000Day + 292194288l) / 146097l);

  476.             // the previous estimate is one unit too high in some rare cases
  477.             // (240 days in the 400 years gregorian cycle, about 0.16%)
  478.             if (j2000Day <= getLastJ2000DayOfYear(year - 1)) {
  479.                 --year;
  480.             }

  481.             // exact year
  482.             return year;

  483.         }

  484.         /** {@inheritDoc} */
  485.         public int getLastJ2000DayOfYear(final int year) {
  486.             return 365 * year + year / 4 - year / 100 + year / 400 - 730120;
  487.         }

  488.         /** {@inheritDoc} */
  489.         public boolean isLeap(final int year) {
  490.             return ((year % 4) == 0) && (((year % 400) == 0) || ((year % 100) != 0));
  491.         }

  492.     }

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

  495.         /** Get the month number for a given day number within year.
  496.          * @param dayInYear day number within year
  497.          * @return month number
  498.          */
  499.         int getMonth(int dayInYear);

  500.         /** Get the day number for given month and day number within year.
  501.          * @param dayInYear day number within year
  502.          * @param month month number
  503.          * @return day number
  504.          */
  505.         int getDay(int dayInYear, int month);

  506.         /** Get the day number within year for given month and day numbers.
  507.          * @param month month number
  508.          * @param day day number
  509.          * @return day number within year
  510.          */
  511.         int getDayInYear(int month, int day);

  512.     }

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

  515.         /** Months succession definition. */
  516.         private static final int[] PREVIOUS_MONTH_END_DAY = {
  517.             0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
  518.         };

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

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

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

  531.     }

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

  534.         /** Months succession definition. */
  535.         private static final int[] PREVIOUS_MONTH_END_DAY = {
  536.             0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  537.         };

  538.         /** {@inheritDoc} */
  539.         public int getMonth(final int dayInYear) {
  540.             return (dayInYear < 32) ? 1 : (10 * dayInYear + 323) / 306;
  541.         }

  542.         /** {@inheritDoc} */
  543.         public int getDay(final int dayInYear, final int month) {
  544.             return dayInYear - PREVIOUS_MONTH_END_DAY[month];
  545.         }

  546.         /** {@inheritDoc} */
  547.         public int getDayInYear(final int month, final int day) {
  548.             return day + PREVIOUS_MONTH_END_DAY[month];
  549.         }

  550.     }

  551. }