1   /* Copyright 2002-2019 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  
19  import java.io.Serializable;
20  import java.text.DecimalFormat;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.orekit.errors.OrekitIllegalArgumentException;
25  import org.orekit.errors.OrekitMessages;
26  
27  /** Class representing a date broken up as year, month and day components.
28   * <p>This class uses the astronomical convention for calendars,
29   * which is also the convention used by <code>java.util.Date</code>:
30   * a year zero is present between years -1 and +1, and 10 days are
31   * missing in 1582. The calendar used around these special dates are:</p>
32   * <ul>
33   *   <li>up to 0000-12-31 : proleptic julian calendar</li>
34   *   <li>from 0001-01-01 to 1582-10-04: julian calendar</li>
35   *   <li>from 1582-10-15: gregorian calendar</li>
36   * </ul>
37   * <p>Instances of this class are guaranteed to be immutable.</p>
38   * @see TimeComponents
39   * @see DateTimeComponents
40   * @author Luc Maisonobe
41   */
42  public class DateComponents implements Serializable, Comparable<DateComponents> {
43  
44      /** Reference epoch for julian dates: -4712-01-01.
45       * <p>Both <code>java.util.Date</code> and {@link DateComponents} classes
46       * follow the astronomical conventions and consider a year 0 between
47       * years -1 and +1, hence this reference date lies in year -4712 and not
48       * in year -4713 as can be seen in other documents or programs that obey
49       * a different convention (for example the <code>convcal</code> utility).</p>
50       */
51      public static final DateComponents JULIAN_EPOCH;
52  
53      /** Reference epoch for modified julian dates: 1858-11-17. */
54      public static final DateComponents MODIFIED_JULIAN_EPOCH;
55  
56      /** Reference epoch for 1950 dates: 1950-01-01. */
57      public static final DateComponents FIFTIES_EPOCH;
58  
59      /** Reference epoch for CCSDS Time Code Format (CCSDS 301.0-B-4): 1958-01-01. */
60      public static final DateComponents CCSDS_EPOCH;
61  
62      /** Reference epoch for Galileo System Time: 1999-08-22. */
63      public static final DateComponents GALILEO_EPOCH;
64  
65      /** Reference epoch for GPS weeks: 1980-01-06. */
66      public static final DateComponents GPS_EPOCH;
67  
68      /** Reference epoch for QZSS weeks: 1980-01-06. */
69      public static final DateComponents QZSS_EPOCH;
70  
71      /** Reference epoch for BeiDou weeks: 2006-01-01. */
72      public static final DateComponents BEIDOU_EPOCH;
73  
74      /** Reference epoch for GLONASS four-year interval number: 1996-01-01. */
75      public static final DateComponents GLONASS_EPOCH;
76  
77      /** J2000.0 Reference epoch: 2000-01-01. */
78      public static final DateComponents J2000_EPOCH;
79  
80      /** Java Reference epoch: 1970-01-01. */
81      public static final DateComponents JAVA_EPOCH;
82  
83      /** Maximum supported date.
84       * <p>
85       * This is date 5881610-07-11 which corresponds to {@code Integer.MAX_VALUE}
86       * days after {@link #J2000_EPOCH}.
87       * </p>
88       * @since 9.0
89       */
90      public static final DateComponents MAX_EPOCH;
91  
92      /** Maximum supported date.
93       * <p>
94       * This is date -5877490-03-03, which corresponds to {@code Integer.MIN_VALUE}
95       * days before {@link #J2000_EPOCH}.
96       * </p>
97       * @since 9.0
98       */
99      public static final DateComponents MIN_EPOCH;
100 
101     /** Serializable UID. */
102     private static final long serialVersionUID = -2462694707837970938L;
103 
104     /** Factory for proleptic julian calendar (up to 0000-12-31). */
105     private static final YearFactory PROLEPTIC_JULIAN_FACTORY = new ProlepticJulianFactory();
106 
107     /** Factory for julian calendar (from 0001-01-01 to 1582-10-04). */
108     private static final YearFactory JULIAN_FACTORY           = new JulianFactory();
109 
110     /** Factory for gregorian calendar (from 1582-10-15). */
111     private static final YearFactory GREGORIAN_FACTORY        = new GregorianFactory();
112 
113     /** Factory for leap years. */
114     private static final MonthDayFactory LEAP_YEAR_FACTORY    = new LeapYearFactory();
115 
116     /** Factory for non-leap years. */
117     private static final MonthDayFactory COMMON_YEAR_FACTORY  = new CommonYearFactory();
118 
119     /** Format for years. */
120     private static final DecimalFormat FOUR_DIGITS = new DecimalFormat("0000");
121 
122     /** Format for months and days. */
123     private static final DecimalFormat TWO_DIGITS  = new DecimalFormat("00");
124 
125     /** Offset between J2000 epoch and modified julian day epoch. */
126     private static final int MJD_TO_J2000 = 51544;
127 
128     /** Basic and extended format calendar date. */
129     private static Pattern CALENDAR_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?(\\d\\d)-?(\\d\\d)$");
130 
131     /** Basic and extended format ordinal date. */
132     private static Pattern ORDINAL_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?(\\d\\d\\d)$");
133 
134     /** Basic and extended format week date. */
135     private static Pattern WEEK_FORMAT = Pattern.compile("^(-?\\d\\d\\d\\d)-?W(\\d\\d)-?(\\d)$");
136 
137     static {
138         // this static statement makes sure the reference epoch are initialized
139         // once AFTER the various factories have been set up
140         JULIAN_EPOCH          = new DateComponents(-4712,  1,  1);
141         MODIFIED_JULIAN_EPOCH = new DateComponents(1858, 11, 17);
142         FIFTIES_EPOCH         = new DateComponents(1950, 1, 1);
143         CCSDS_EPOCH           = new DateComponents(1958, 1, 1);
144         GALILEO_EPOCH         = new DateComponents(1999, 8, 22);
145         GPS_EPOCH             = new DateComponents(1980, 1, 6);
146         QZSS_EPOCH            = new DateComponents(1980, 1, 6);
147         BEIDOU_EPOCH          = new DateComponents(2006, 1, 1);
148         GLONASS_EPOCH         = new DateComponents(1996, 1, 1);
149         J2000_EPOCH           = new DateComponents(2000, 1, 1);
150         JAVA_EPOCH            = new DateComponents(1970, 1, 1);
151         MAX_EPOCH             = new DateComponents(Integer.MAX_VALUE);
152         MIN_EPOCH             = new DateComponents(Integer.MIN_VALUE);
153     }
154 
155     /** Year number. */
156     private final int year;
157 
158     /** Month number. */
159     private final int month;
160 
161     /** Day number. */
162     private final int day;
163 
164     /** Build a date from its components.
165      * @param year year number (may be 0 or negative for BC years)
166      * @param month month number from 1 to 12
167      * @param day day number from 1 to 31
168      * @exception IllegalArgumentException if inconsistent arguments
169      * are given (parameters out of range, february 29 for non-leap years,
170      * dates during the gregorian leap in 1582 ...)
171      */
172     public DateComponents(final int year, final int month, final int day)
173         throws IllegalArgumentException {
174 
175         // very rough range check
176         // (just to avoid ArrayOutOfboundException in MonthDayFactory later)
177         if ((month < 1) || (month > 12)) {
178             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_MONTH, month);
179         }
180 
181         // start by trusting the parameters
182         this.year  = year;
183         this.month = month;
184         this.day   = day;
185 
186         // build a check date from the J2000 day
187         final DateComponentsteComponents">DateComponents check = new DateComponents(getJ2000Day());
188 
189         // check the parameters for mismatch
190         // (i.e. invalid date components, like 29 february on non-leap years)
191         if ((year != check.year) || (month != check.month) || (day != check.day)) {
192             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_YEAR_MONTH_DAY,
193                                                       year, month, day);
194         }
195 
196     }
197 
198     /** Build a date from its components.
199      * @param year year number (may be 0 or negative for BC years)
200      * @param month month enumerate
201      * @param day day number from 1 to 31
202      * @exception IllegalArgumentException if inconsistent arguments
203      * are given (parameters out of range, february 29 for non-leap years,
204      * dates during the gregorian leap in 1582 ...)
205      */
206     public DateComponents(final int year, final Month month, final int day)
207         throws IllegalArgumentException {
208         this(year, month.getNumber(), day);
209     }
210 
211     /** Build a date from a year and day number.
212      * @param year year number (may be 0 or negative for BC years)
213      * @param dayNumber day number in the year from 1 to 366
214      * @exception IllegalArgumentException if dayNumber is out of range
215      * with respect to year
216      */
217     public DateComponents(final int year, final int dayNumber)
218         throws IllegalArgumentException {
219         this(J2000_EPOCH, new DateComponents(year - 1, 12, 31).getJ2000Day() + dayNumber);
220         if (dayNumber != getDayOfYear()) {
221             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_DAY_NUMBER_IN_YEAR,
222                                                      dayNumber, year);
223         }
224     }
225 
226     /** Build a date from its offset with respect to a {@link #J2000_EPOCH}.
227      * @param offset offset with respect to a {@link #J2000_EPOCH}
228      * @see #getJ2000Day()
229      */
230     public DateComponents(final int offset) {
231 
232         // we follow the astronomical convention for calendars:
233         // we consider a year zero and 10 days are missing in 1582
234         // from 1582-10-15: gregorian calendar
235         // from 0001-01-01 to 1582-10-04: julian calendar
236         // up to 0000-12-31 : proleptic julian calendar
237         YearFactory yFactory = GREGORIAN_FACTORY;
238         if (offset < -152384) {
239             if (offset > -730122) {
240                 yFactory = JULIAN_FACTORY;
241             } else {
242                 yFactory = PROLEPTIC_JULIAN_FACTORY;
243             }
244         }
245         year = yFactory.getYear(offset);
246         final int dayInYear = offset - yFactory.getLastJ2000DayOfYear(year - 1);
247 
248         // handle month/day according to the year being a common or leap year
249         final MonthDayFactory mdFactory =
250             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
251         month = mdFactory.getMonth(dayInYear);
252         day   = mdFactory.getDay(dayInYear, month);
253 
254     }
255 
256     /** Build a date from its offset with respect to a reference epoch.
257      * <p>This constructor is mainly useful to build a date from a modified
258      * julian day (using {@link #MODIFIED_JULIAN_EPOCH}) or a GPS week number
259      * (using {@link #GPS_EPOCH}).</p>
260      * @param epoch reference epoch
261      * @param offset offset with respect to a reference epoch
262      * @see #DateComponents(int)
263      * @see #getMJD()
264      */
265     public DateComponentstml#DateComponents">DateComponents(final DateComponents epoch, final int offset) {
266         this(epoch.getJ2000Day() + offset);
267     }
268 
269     /** Build a date from week components.
270      * <p>The calendar week number is a number between 1 and 52 or 53 depending
271      * on the year. Week 1 is defined by ISO as the one that includes the first
272      * Thursday of a year. Week 1 may therefore start the previous year and week
273      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
274      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
275      * is in fact the first day of year 1995). This date would beAnother example is calendar date
276      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
277      * first week of 1997 is in fact the last day of year 1996).</p>
278      * @param wYear year associated to week numbering
279      * @param week week number in year, from 1 to 52 or 53
280      * @param dayOfWeek day of week, from 1 (Monday) to 7 (Sunday)
281      * @return a builded date
282      * @exception IllegalArgumentException if inconsistent arguments
283      * are given (parameters out of range, week 53 on a 52 weeks year ...)
284      */
285     public static DateComponents createFromWeekComponents(final int wYear, final int week, final int dayOfWeek)
286         throws IllegalArgumentException {
287 
288         final DateComponentsts">DateComponents firstWeekMonday = new DateComponents(getFirstWeekMonday(wYear));
289         final DateComponentsl#DateComponents">DateComponents d = new DateComponents(firstWeekMonday, 7 * week + dayOfWeek - 8);
290 
291         // check the parameters for invalid date components
292         if ((week != d.getCalendarWeek()) || (dayOfWeek != d.getDayOfWeek())) {
293             throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_WEEK_DATE,
294                                                      wYear, week, dayOfWeek);
295         }
296 
297         return d;
298 
299     }
300 
301     /** Parse a string in ISO-8601 format to build a date.
302      * <p>The supported formats are:
303      * <ul>
304      *   <li>basic format calendar date: YYYYMMDD</li>
305      *   <li>extended format calendar date: YYYY-MM-DD</li>
306      *   <li>basic format ordinal date: YYYYDDD</li>
307      *   <li>extended format ordinal date: YYYY-DDD</li>
308      *   <li>basic format week date: YYYYWwwD</li>
309      *   <li>extended format week date: YYYY-Www-D</li>
310      * </ul>
311      *
312      * <p> As shown by the list above, only the complete representations defined in section 4.1
313      * of ISO-8601 standard are supported, neither expended representations nor representations
314      * with reduced accuracy are supported.
315      *
316      * <p>
317      * Parsing a single integer as a julian day is <em>not</em> supported as it may be ambiguous
318      * with either the basic format calendar date or the basic format ordinal date depending
319      * on the number of digits.
320      * </p>
321      * @param string string to parse
322      * @return a parsed date
323      * @exception IllegalArgumentException if string cannot be parsed
324      */
325     public static  DateComponents parseDate(final String string) {
326 
327         // is the date a calendar date ?
328         final Matcher calendarMatcher = CALENDAR_FORMAT.matcher(string);
329         if (calendarMatcher.matches()) {
330             return new DateComponents(Integer.parseInt(calendarMatcher.group(1)),
331                                       Integer.parseInt(calendarMatcher.group(2)),
332                                       Integer.parseInt(calendarMatcher.group(3)));
333         }
334 
335         // is the date an ordinal date ?
336         final Matcher ordinalMatcher = ORDINAL_FORMAT.matcher(string);
337         if (ordinalMatcher.matches()) {
338             return new DateComponents(Integer.parseInt(ordinalMatcher.group(1)),
339                                       Integer.parseInt(ordinalMatcher.group(2)));
340         }
341 
342         // is the date a week date ?
343         final Matcher weekMatcher = WEEK_FORMAT.matcher(string);
344         if (weekMatcher.matches()) {
345             return createFromWeekComponents(Integer.parseInt(weekMatcher.group(1)),
346                                             Integer.parseInt(weekMatcher.group(2)),
347                                             Integer.parseInt(weekMatcher.group(3)));
348         }
349 
350         throw new OrekitIllegalArgumentException(OrekitMessages.NON_EXISTENT_DATE, string);
351 
352     }
353 
354     /** Get the year number.
355      * @return year number (may be 0 or negative for BC years)
356      */
357     public int getYear() {
358         return year;
359     }
360 
361     /** Get the month.
362      * @return month number from 1 to 12
363      */
364     public int getMonth() {
365         return month;
366     }
367 
368     /** Get the month as an enumerate.
369      * @return month as an enumerate
370      */
371     public Month getMonthEnum() {
372         return Month.getMonth(month);
373     }
374 
375     /** Get the day.
376      * @return day number from 1 to 31
377      */
378     public int getDay() {
379         return day;
380     }
381 
382     /** Get the day number with respect to J2000 epoch.
383      * @return day number with respect to J2000 epoch
384      */
385     public int getJ2000Day() {
386         YearFactory yFactory = GREGORIAN_FACTORY;
387         if (year < 1583) {
388             if (year < 1) {
389                 yFactory = PROLEPTIC_JULIAN_FACTORY;
390             } else if ((year < 1582) || (month < 10) || ((month < 11) && (day < 5))) {
391                 yFactory = JULIAN_FACTORY;
392             }
393         }
394         final MonthDayFactory mdFactory =
395             yFactory.isLeap(year) ? LEAP_YEAR_FACTORY : COMMON_YEAR_FACTORY;
396         return yFactory.getLastJ2000DayOfYear(year - 1) +
397                mdFactory.getDayInYear(month, day);
398     }
399 
400     /** Get the modified julian day.
401      * @return modified julian day
402      */
403     public int getMJD() {
404         return MJD_TO_J2000 + getJ2000Day();
405     }
406 
407     /** Get the calendar week number.
408      * <p>The calendar week number is a number between 1 and 52 or 53 depending
409      * on the year. Week 1 is defined by ISO as the one that includes the first
410      * Thursday of a year. Week 1 may therefore start the previous year and week
411      * 52 or 53 may end in the next year. As an example calendar date 1995-01-01
412      * corresponds to week date 1994-W52-7 (i.e. Sunday in the last week of 1994
413      * is in fact the first day of year 1995). Another example is calendar date
414      * 1996-12-31 which corresponds to week date 1997-W01-2 (i.e. Tuesday in the
415      * first week of 1997 is in fact the last day of year 1996).</p>
416      * @return calendar week number
417      */
418     public int getCalendarWeek() {
419         final int firstWeekMonday = getFirstWeekMonday(year);
420         int daysSincefirstMonday = getJ2000Day() - firstWeekMonday;
421         if (daysSincefirstMonday < 0) {
422             // we are still in a week from previous year
423             daysSincefirstMonday += firstWeekMonday - getFirstWeekMonday(year - 1);
424         } else if (daysSincefirstMonday > 363) {
425             // up to three days at end of year may belong to first week of next year
426             // (by chance, there is no need for a specific check in year 1582 ...)
427             final int weekYearLength = getFirstWeekMonday(year + 1) - firstWeekMonday;
428             if (daysSincefirstMonday >= weekYearLength) {
429                 daysSincefirstMonday -= weekYearLength;
430             }
431         }
432         return 1 + daysSincefirstMonday / 7;
433     }
434 
435     /** Get the monday of a year first week.
436      * @param year year to consider
437      * @return day of the monday of the first weak of year
438      */
439     private static int getFirstWeekMonday(final int year) {
440         final int yearFirst = new DateComponents(year, 1, 1).getJ2000Day();
441         final int offsetToMonday = 4 - (yearFirst + 2) % 7;
442         return yearFirst + offsetToMonday + ((offsetToMonday > 3) ? -7 : 0);
443     }
444 
445     /** Get the day of week.
446      * <p>Day of week is a number between 1 (Monday) and 7 (Sunday).</p>
447      * @return day of week
448      */
449     public int getDayOfWeek() {
450         final int dow = (getJ2000Day() + 6) % 7; // result is between -6 and +6
451         return (dow < 1) ? (dow + 7) : dow;
452     }
453 
454     /** Get the day number in year.
455      * <p>Day number in year is between 1 (January 1st) and either 365 or
456      * 366 inclusive depending on year.</p>
457      * @return day number in year
458      */
459     public int getDayOfYear() {
460         return getJ2000Day() - new DateComponents(year - 1, 12, 31).getJ2000Day();
461     }
462 
463     /** Get a string representation (ISO-8601) of the date.
464      * @return string representation of the date.
465      */
466     public String toString() {
467         return new StringBuffer().
468                append(FOUR_DIGITS.format(year)).append('-').
469                append(TWO_DIGITS.format(month)).append('-').
470                append(TWO_DIGITS.format(day)).
471                toString();
472     }
473 
474     /** {@inheritDoc} */
475     public int compareTo(final DateComponents other) {
476         final int j2000Day = getJ2000Day();
477         final int otherJ2000Day = other.getJ2000Day();
478         if (j2000Day < otherJ2000Day) {
479             return -1;
480         } else if (j2000Day > otherJ2000Day) {
481             return 1;
482         }
483         return 0;
484     }
485 
486     /** {@inheritDoc} */
487     public boolean equals(final Object other) {
488         try {
489             final DateComponentsg/orekit/time/DateComponents.html#DateComponents">DateComponents otherDate = (DateComponents) other;
490             return (otherDate != null) && (year == otherDate.year) &&
491                    (month == otherDate.month) && (day == otherDate.day);
492         } catch (ClassCastException cce) {
493             return false;
494         }
495     }
496 
497     /** {@inheritDoc} */
498     public int hashCode() {
499         return (year << 16) ^ (month << 8) ^ day;
500     }
501 
502     /** Interface for dealing with years sequences according to some calendar. */
503     private interface YearFactory {
504 
505         /** Get the year number for a given day number with respect to J2000 epoch.
506          * @param j2000Day day number with respect to J2000 epoch
507          * @return year number
508          */
509         int getYear(int j2000Day);
510 
511         /** Get the day number with respect to J2000 epoch for new year's Eve.
512          * @param year year number
513          * @return day number with respect to J2000 epoch for new year's Eve
514          */
515         int getLastJ2000DayOfYear(int year);
516 
517         /** Check if a year is a leap or common year.
518          * @param year year number
519          * @return true if year is a leap year
520          */
521         boolean isLeap(int year);
522 
523     }
524 
525     /** Class providing a years sequence compliant with the proleptic Julian calendar. */
526     private static class ProlepticJulianFactory implements YearFactory {
527 
528         /** {@inheritDoc} */
529         public int getYear(final int j2000Day) {
530             return  (int) -((-4l * j2000Day - 2920488l) / 1461l);
531         }
532 
533         /** {@inheritDoc} */
534         public int getLastJ2000DayOfYear(final int year) {
535             return 365 * year + (year + 1) / 4 - 730123;
536         }
537 
538         /** {@inheritDoc} */
539         public boolean isLeap(final int year) {
540             return (year % 4) == 0;
541         }
542 
543     }
544 
545     /** Class providing a years sequence compliant with the Julian calendar. */
546     private static class JulianFactory implements YearFactory {
547 
548         /** {@inheritDoc} */
549         public int getYear(final int j2000Day) {
550             return  (int) ((4l * j2000Day + 2921948l) / 1461l);
551         }
552 
553         /** {@inheritDoc} */
554         public int getLastJ2000DayOfYear(final int year) {
555             return 365 * year + year / 4 - 730122;
556         }
557 
558         /** {@inheritDoc} */
559         public boolean isLeap(final int year) {
560             return (year % 4) == 0;
561         }
562 
563     }
564 
565     /** Class providing a years sequence compliant with the Gregorian calendar. */
566     private static class GregorianFactory implements YearFactory {
567 
568         /** {@inheritDoc} */
569         public int getYear(final int j2000Day) {
570 
571             // year estimate
572             int year = (int) ((400l * j2000Day + 292194288l) / 146097l);
573 
574             // the previous estimate is one unit too high in some rare cases
575             // (240 days in the 400 years gregorian cycle, about 0.16%)
576             if (j2000Day <= getLastJ2000DayOfYear(year - 1)) {
577                 --year;
578             }
579 
580             // exact year
581             return year;
582 
583         }
584 
585         /** {@inheritDoc} */
586         public int getLastJ2000DayOfYear(final int year) {
587             return 365 * year + year / 4 - year / 100 + year / 400 - 730120;
588         }
589 
590         /** {@inheritDoc} */
591         public boolean isLeap(final int year) {
592             return ((year % 4) == 0) && (((year % 400) == 0) || ((year % 100) != 0));
593         }
594 
595     }
596 
597     /** Interface for dealing with months sequences according to leap/common years. */
598     private interface MonthDayFactory {
599 
600         /** Get the month number for a given day number within year.
601          * @param dayInYear day number within year
602          * @return month number
603          */
604         int getMonth(int dayInYear);
605 
606         /** Get the day number for given month and day number within year.
607          * @param dayInYear day number within year
608          * @param month month number
609          * @return day number
610          */
611         int getDay(int dayInYear, int month);
612 
613         /** Get the day number within year for given month and day numbers.
614          * @param month month number
615          * @param day day number
616          * @return day number within year
617          */
618         int getDayInYear(int month, int day);
619 
620     }
621 
622     /** Class providing the months sequence for leap years. */
623     private static class LeapYearFactory implements MonthDayFactory {
624 
625         /** Months succession definition. */
626         private static final int[] PREVIOUS_MONTH_END_DAY = {
627             0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
628         };
629 
630         /** {@inheritDoc} */
631         public int getMonth(final int dayInYear) {
632             return (dayInYear < 32) ? 1 : (10 * dayInYear + 313) / 306;
633         }
634 
635         /** {@inheritDoc} */
636         public int getDay(final int dayInYear, final int month) {
637             return dayInYear - PREVIOUS_MONTH_END_DAY[month];
638         }
639 
640         /** {@inheritDoc} */
641         public int getDayInYear(final int month, final int day) {
642             return day + PREVIOUS_MONTH_END_DAY[month];
643         }
644 
645     }
646 
647     /** Class providing the months sequence for common years. */
648     private static class CommonYearFactory implements MonthDayFactory {
649 
650         /** Months succession definition. */
651         private static final int[] PREVIOUS_MONTH_END_DAY = {
652             0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
653         };
654 
655         /** {@inheritDoc} */
656         public int getMonth(final int dayInYear) {
657             return (dayInYear < 32) ? 1 : (10 * dayInYear + 323) / 306;
658         }
659 
660         /** {@inheritDoc} */
661         public int getDay(final int dayInYear, final int month) {
662             return dayInYear - PREVIOUS_MONTH_END_DAY[month];
663         }
664 
665         /** {@inheritDoc} */
666         public int getDayInYear(final int month, final int day) {
667             return day + PREVIOUS_MONTH_END_DAY[month];
668         }
669 
670     }
671 
672 }