1 16 package org.joda.time.chrono; 17 18 import java.util.Locale ; 19 20 import org.joda.time.DateTimeConstants; 21 import org.joda.time.DateTimeFieldType; 22 import org.joda.time.DurationField; 23 import org.joda.time.DurationFieldType; 24 import org.joda.time.field.BaseDateTimeField; 25 import org.joda.time.field.FieldUtils; 26 import org.joda.time.field.UnsupportedDurationField; 27 28 35 final class GJEraDateTimeField extends BaseDateTimeField { 36 37 38 private static final long serialVersionUID = 4240986525305515528L; 39 40 private final BasicChronology iChronology; 41 42 45 GJEraDateTimeField(BasicChronology chronology) { 46 super(DateTimeFieldType.era()); 47 iChronology = chronology; 48 } 49 50 public boolean isLenient() { 51 return false; 52 } 53 54 59 public int get(long instant) { 60 if (iChronology.getYear(instant) <= 0) { 61 return DateTimeConstants.BCE; 62 } else { 63 return DateTimeConstants.CE; 64 } 65 } 66 67 public String getAsText(int fieldValue, Locale locale) { 68 return GJLocaleSymbols.forLocale(locale).eraValueToText(fieldValue); 69 } 70 71 79 public long set(long instant, int era) { 80 FieldUtils.verifyValueBounds(this, era, DateTimeConstants.BCE, DateTimeConstants.CE); 81 82 int oldEra = get(instant); 83 if (oldEra != era) { 84 int year = iChronology.getYear(instant); 85 return iChronology.setYear(instant, -year); 86 } else { 87 return instant; 88 } 89 } 90 91 public long set(long instant, String text, Locale locale) { 92 return set(instant, GJLocaleSymbols.forLocale(locale).eraTextToValue(text)); 93 } 94 95 public long roundFloor(long instant) { 96 if (get(instant) == DateTimeConstants.CE) { 97 return iChronology.setYear(0, 1); 98 } else { 99 return Long.MIN_VALUE; 100 } 101 } 102 103 public long roundCeiling(long instant) { 104 if (get(instant) == DateTimeConstants.BCE) { 105 return iChronology.setYear(0, 1); 106 } else { 107 return Long.MAX_VALUE; 108 } 109 } 110 111 public long roundHalfFloor(long instant) { 112 return roundFloor(instant); 114 } 115 116 public long roundHalfCeiling(long instant) { 117 return roundFloor(instant); 119 } 120 121 public long roundHalfEven(long instant) { 122 return roundFloor(instant); 124 } 125 126 public DurationField getDurationField() { 127 return UnsupportedDurationField.getInstance(DurationFieldType.eras()); 128 } 129 130 public DurationField getRangeDurationField() { 131 return null; 132 } 133 134 public int getMinimumValue() { 135 return DateTimeConstants.BCE; 136 } 137 138 public int getMaximumValue() { 139 return DateTimeConstants.CE; 140 } 141 142 public int getMaximumTextLength(Locale locale) { 143 return GJLocaleSymbols.forLocale(locale).getEraMaxTextLength(); 144 } 145 146 149 private Object readResolve() { 150 return iChronology.era(); 151 } 152 } 153 | Popular Tags |