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