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