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