1 16 package org.joda.time.chrono; 17 18 import org.joda.time.DateTimeFieldType; 19 import org.joda.time.DurationField; 20 import org.joda.time.field.FieldUtils; 21 import org.joda.time.field.ImpreciseDateTimeField; 22 23 31 class BasicYearDateTimeField extends ImpreciseDateTimeField { 32 33 34 private static final long serialVersionUID = -98628754872287L; 35 36 37 protected final BasicChronology iChronology; 38 39 44 BasicYearDateTimeField(BasicChronology chronology) { 45 super(DateTimeFieldType.year(), chronology.getAverageMillisPerYear()); 46 iChronology = chronology; 47 } 48 49 public boolean isLenient() { 50 return false; 51 } 52 53 public int get(long instant) { 54 return iChronology.getYear(instant); 55 } 56 57 public long add(long instant, int years) { 58 if (years == 0) { 59 return instant; 60 } 61 int thisYear = get(instant); 62 int newYear = FieldUtils.safeAdd(thisYear, years); 63 return set(instant, newYear); 64 } 65 66 public long add(long instant, long years) { 67 return add(instant, FieldUtils.safeToInt(years)); 68 } 69 70 public long addWrapField(long instant, int years) { 71 if (years == 0) { 72 return instant; 73 } 74 int thisYear = iChronology.getYear(instant); 76 int wrappedYear = FieldUtils.getWrappedValue 77 (thisYear, years, iChronology.getMinYear(), iChronology.getMaxYear()); 78 return set(instant, wrappedYear); 79 } 80 81 public long set(long instant, int year) { 82 FieldUtils.verifyValueBounds 83 (this, year, iChronology.getMinYear(), iChronology.getMaxYear()); 84 return iChronology.setYear(instant, year); 85 } 86 87 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 88 if (minuendInstant < subtrahendInstant) { 89 return -iChronology.getYearDifference(subtrahendInstant, minuendInstant); 90 } 91 return iChronology.getYearDifference(minuendInstant, subtrahendInstant); 92 } 93 94 public DurationField getRangeDurationField() { 95 return null; 96 } 97 98 public boolean isLeap(long instant) { 99 return iChronology.isLeapYear(get(instant)); 100 } 101 102 public int getLeapAmount(long instant) { 103 if (iChronology.isLeapYear(get(instant))) { 104 return 1; 105 } else { 106 return 0; 107 } 108 } 109 110 public DurationField getLeapDurationField() { 111 return iChronology.days(); 112 } 113 114 public int getMinimumValue() { 115 return iChronology.getMinYear(); 116 } 117 118 public int getMaximumValue() { 119 return iChronology.getMaxYear(); 120 } 121 122 public long roundFloor(long instant) { 123 return iChronology.getYearMillis(get(instant)); 124 } 125 126 public long roundCeiling(long instant) { 127 int year = get(instant); 128 long yearStartMillis = iChronology.getYearMillis(year); 129 if (instant != yearStartMillis) { 130 instant = iChronology.getYearMillis(year + 1); 132 } 133 return instant; 134 } 135 136 public long remainder(long instant) { 137 return instant - roundFloor(instant); 138 } 139 140 143 private Object readResolve() { 144 return iChronology.year(); 145 } 146 } 147 | Popular Tags |