1 16 package org.joda.time.chrono; 17 18 import org.joda.time.Chronology; 19 import org.joda.time.DateTimeConstants; 20 21 35 abstract class BasicFixedMonthChronology extends BasicChronology { 36 37 38 private static final long serialVersionUID = 261387371998L; 39 40 41 static final int MONTH_LENGTH = 30; 42 43 44 static final long MILLIS_PER_YEAR = 45 (long) (365.25 * DateTimeConstants.MILLIS_PER_DAY); 46 47 48 static final long MILLIS_PER_MONTH = ((long) MONTH_LENGTH) * DateTimeConstants.MILLIS_PER_DAY; 49 50 58 BasicFixedMonthChronology(Chronology base, Object param, int minDaysInFirstWeek) { 59 super(base, param, minDaysInFirstWeek); 60 } 61 62 long setYear(long instant, int year) { 64 int thisYear = getYear(instant); 66 int dayOfYear = getDayOfYear(instant, thisYear); 67 int millisOfDay = getMillisOfDay(instant); 68 69 if (dayOfYear > 365) { 70 if (!isLeapYear(year)) { 72 dayOfYear--; 74 } 75 } 76 77 instant = getYearMonthDayMillis(year, 1, dayOfYear); 78 instant += millisOfDay; 79 return instant; 80 } 81 82 long getYearDifference(long minuendInstant, long subtrahendInstant) { 84 int minuendYear = getYear(minuendInstant); 86 int subtrahendYear = getYear(subtrahendInstant); 87 88 long minuendRem = minuendInstant - getYearMillis(minuendYear); 90 long subtrahendRem = subtrahendInstant - getYearMillis(subtrahendYear); 91 92 int difference = minuendYear - subtrahendYear; 93 if (minuendRem < subtrahendRem) { 94 difference--; 95 } 96 return difference; 97 } 98 99 long getTotalMillisByYearMonth(int year, int month) { 101 return ((month - 1) * MILLIS_PER_MONTH); 102 } 103 104 int getDayOfMonth(long millis) { 106 return (getDayOfYear(millis) - 1) % MONTH_LENGTH + 1; 108 } 109 110 boolean isLeapYear(int year) { 112 return (year & 3) == 3; 113 } 114 115 int getDaysInYearMonth(int year, int month) { 117 return (month != 13) ? MONTH_LENGTH : (isLeapYear(year) ? 6 : 5); 118 } 119 120 int getDaysInMonthMax() { 122 return MONTH_LENGTH; 123 } 124 125 int getDaysInMonthMax(int month) { 127 return (month != 13 ? MONTH_LENGTH : 6); 128 } 129 130 int getMonthOfYear(long millis) { 132 return (getDayOfYear(millis) - 1) / MONTH_LENGTH + 1; 133 } 134 135 int getMonthOfYear(long millis, int year) { 137 long monthZeroBased = (millis - getYearMillis(year)) / MILLIS_PER_MONTH; 138 return ((int) monthZeroBased) + 1; 139 } 140 141 int getMaxMonth() { 143 return 13; 144 } 145 146 long getAverageMillisPerYear() { 148 return MILLIS_PER_YEAR; 149 } 150 151 long getAverageMillisPerYearDividedByTwo() { 153 return MILLIS_PER_YEAR / 2; 154 } 155 156 long getAverageMillisPerMonth() { 158 return MILLIS_PER_MONTH; 159 } 160 161 } 162 | Popular Tags |