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