1 56 57 package org.jfree.data.time; 58 59 import java.io.Serializable ; 60 import java.util.Calendar ; 61 import java.util.Date ; 62 import java.util.TimeZone ; 63 64 import org.jfree.date.MonthConstants; 65 import org.jfree.date.SerialDate; 66 67 71 public class Year extends RegularTimePeriod implements Serializable { 72 73 74 private static final long serialVersionUID = -7659990929736074836L; 75 76 77 private int year; 78 79 82 public Year() { 83 this(new Date ()); 84 } 85 86 91 public Year(int year) { 92 93 if ((year < SerialDate.MINIMUM_YEAR_SUPPORTED) 95 || (year > SerialDate.MAXIMUM_YEAR_SUPPORTED)) { 96 97 throw new IllegalArgumentException ( 98 "Year constructor: year (" + year + ") outside valid range."); 99 } 100 101 this.year = year; 103 104 } 105 106 112 public Year(Date time) { 113 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 114 } 115 116 122 public Year(Date time, TimeZone zone) { 123 124 Calendar calendar = Calendar.getInstance(zone); 125 calendar.setTime(time); 126 this.year = calendar.get(Calendar.YEAR); 127 128 } 129 130 135 public int getYear() { 136 return this.year; 137 } 138 139 145 public RegularTimePeriod previous() { 146 if (this.year > SerialDate.MINIMUM_YEAR_SUPPORTED) { 147 return new Year(this.year - 1); 148 } 149 else { 150 return null; 151 } 152 } 153 154 160 public RegularTimePeriod next() { 161 if (this.year < SerialDate.MAXIMUM_YEAR_SUPPORTED) { 162 return new Year(this.year + 1); 163 } 164 else { 165 return null; 166 } 167 } 168 169 176 public long getSerialIndex() { 177 return this.year; 178 } 179 180 188 public long getFirstMillisecond(Calendar calendar) { 189 Day jan1 = new Day(1, MonthConstants.JANUARY, this.year); 190 return jan1.getFirstMillisecond(calendar); 191 } 192 193 201 public long getLastMillisecond(Calendar calendar) { 202 Day dec31 = new Day(31, MonthConstants.DECEMBER, this.year); 203 return dec31.getLastMillisecond(calendar); 204 } 205 206 217 public boolean equals(Object object) { 218 if (object != null) { 219 if (object instanceof Year) { 220 Year target = (Year) object; 221 return (this.year == target.getYear()); 222 } 223 else { 224 return false; 225 } 226 } 227 else { 228 return false; 229 } 230 } 231 232 241 public int hashCode() { 242 int result = 17; 243 int c = this.year; 244 result = 37 * result + c; 245 return result; 246 } 247 248 258 public int compareTo(Object o1) { 259 260 int result; 261 262 if (o1 instanceof Year) { 265 Year y = (Year) o1; 266 result = this.year - y.getYear(); 267 } 268 269 else if (o1 instanceof RegularTimePeriod) { 272 result = 0; 274 } 275 276 else { 279 result = 1; 281 } 282 283 return result; 284 285 } 286 287 292 public String toString() { 293 return Integer.toString(this.year); 294 } 295 296 306 public static Year parseYear(String s) { 307 308 int y; 310 try { 311 y = Integer.parseInt(s.trim()); 312 } 313 catch (NumberFormatException e) { 314 throw new TimePeriodFormatException("Cannot parse string."); 315 } 316 317 try { 319 return new Year(y); 320 } 321 catch (IllegalArgumentException e) { 322 throw new TimePeriodFormatException("Year outside valid range."); 323 } 324 } 325 326 } 327 | Popular Tags |