1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package java.time.chrono; 58 59 import static java.time.chrono.MinguoChronology.YEARS_DIFFERENCE; 60 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 62 import static java.time.temporal.ChronoField.YEAR; 63 64 import java.io.DataInput; 65 import java.io.DataOutput; 66 import java.io.IOException; 67 import java.io.Serializable; 68 import java.time.Clock; 69 import java.time.DateTimeException; 70 import java.time.LocalDate; 71 import java.time.LocalTime; 72 import java.time.Period; 73 import java.time.ZoneId; 74 import java.time.temporal.ChronoField; 75 import java.time.temporal.TemporalAccessor; 76 import java.time.temporal.TemporalAdjuster; 77 import java.time.temporal.TemporalAmount; 78 import java.time.temporal.TemporalField; 79 import java.time.temporal.TemporalQuery; 80 import java.time.temporal.TemporalUnit; 81 import java.time.temporal.UnsupportedTemporalTypeException; 82 import java.time.temporal.ValueRange; 83 import java.util.Objects; 84 85 /** 86 * A date in the Minguo calendar system. 87 * <p> 88 * This date operates using the {@linkplain MinguoChronology Minguo calendar}. 89 * This calendar system is primarily used in the Republic of China, often known as Taiwan. 90 * Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}. 91 * 92 * <h3>Specification for implementors</h3> 93 * This class is immutable and thread-safe. 94 * 95 * @since 1.8 96 */ 97 public final class MinguoDate 98 extends ChronoDateImpl<MinguoDate> 99 implements ChronoLocalDate<MinguoDate>, Serializable { 100 101 /** 102 * Serialization version. 103 */ 104 private static final long serialVersionUID = 1300372329181994526L; 105 106 /** 107 * The underlying date. 108 */ 109 private final LocalDate isoDate; 110 111 //----------------------------------------------------------------------- 112 /** 113 * Obtains the current {@code MinguoDate} from the system clock in the default time-zone. 114 * <p> 115 * This will query the {@link Clock#systemDefaultZone() system clock} in the default 116 * time-zone to obtain the current date. 117 * <p> 118 * Using this method will prevent the ability to use an alternate clock for testing 119 * because the clock is hard-coded. 120 * 121 * @return the current date using the system clock and default time-zone, not null 122 */ 123 public static MinguoDate now() { 124 return now(Clock.systemDefaultZone()); 125 } 126 127 /** 128 * Obtains the current {@code MinguoDate} from the system clock in the specified time-zone. 129 * <p> 130 * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. 131 * Specifying the time-zone avoids dependence on the default time-zone. 132 * <p> 133 * Using this method will prevent the ability to use an alternate clock for testing 134 * because the clock is hard-coded. 135 * 136 * @param zone the zone ID to use, not null 137 * @return the current date using the system clock, not null 138 */ 139 public static MinguoDate now(ZoneId zone) { 140 return now(Clock.system(zone)); 141 } 142 143 /** 144 * Obtains the current {@code MinguoDate} from the specified clock. 145 * <p> 146 * This will query the specified clock to obtain the current date - today. 147 * Using this method allows the use of an alternate clock for testing. 148 * The alternate clock may be introduced using {@linkplain Clock dependency injection}. 149 * 150 * @param clock the clock to use, not null 151 * @return the current date, not null 152 * @throws DateTimeException if the current date cannot be obtained 153 */ 154 public static MinguoDate now(Clock clock) { 155 return MinguoChronology.INSTANCE.date(LocalDate.now(clock)); 156 } 157 158 /** 159 * Obtains a {@code MinguoDate} representing a date in the Minguo calendar 160 * system from the proleptic-year, month-of-year and day-of-month fields. 161 * <p> 162 * This returns a {@code MinguoDate} with the specified fields. 163 * The day must be valid for the year and month, otherwise an exception will be thrown. 164 * 165 * @param prolepticYear the Minguo proleptic-year 166 * @param month the Minguo month-of-year, from 1 to 12 167 * @param dayOfMonth the Minguo day-of-month, from 1 to 31 168 * @return the date in Minguo calendar system, not null 169 * @throws DateTimeException if the value of any field is out of range, 170 * or if the day-of-month is invalid for the month-year 171 */ 172 public static MinguoDate of(int prolepticYear, int month, int dayOfMonth) { 173 return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth)); 174 } 175 176 /** 177 * Obtains a {@code MinguoDate} from a temporal object. 178 * <p> 179 * This obtains a date in the Minguo calendar system based on the specified temporal. 180 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, 181 * which this factory converts to an instance of {@code MinguoDate}. 182 * <p> 183 * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} 184 * field, which is standardized across calendar systems. 185 * <p> 186 * This method matches the signature of the functional interface {@link TemporalQuery} 187 * allowing it to be used as a query via method reference, {@code MinguoDate::from}. 188 * 189 * @param temporal the temporal object to convert, not null 190 * @return the date in Minguo calendar system, not null 191 * @throws DateTimeException if unable to convert to a {@code MinguoDate} 192 */ 193 public static MinguoDate from(TemporalAccessor temporal) { 194 return MinguoChronology.INSTANCE.date(temporal); 195 } 196 197 //----------------------------------------------------------------------- 198 /** 199 * Creates an instance from an ISO date. 200 * 201 * @param isoDate the standard local date, validated not null 202 */ 203 MinguoDate(LocalDate isoDate) { 204 Objects.requireNonNull(isoDate, "isoDate"); 205 this.isoDate = isoDate; 206 } 207 208 //----------------------------------------------------------------------- 209 /** 210 * Gets the chronology of this date, which is the Minguo calendar system. 211 * <p> 212 * The {@code Chronology} represents the calendar system in use. 213 * The era and other fields in {@link ChronoField} are defined by the chronology. 214 * 215 * @return the Minguo chronology, not null 216 */ 217 @Override 218 public MinguoChronology getChronology() { 219 return MinguoChronology.INSTANCE; 220 } 221 222 /** 223 * Gets the era applicable at this date. 224 * <p> 225 * The Minguo calendar system has two eras, 'ROC' and 'BEFORE_ROC', 226 * defined by {@link MinguoEra}. 227 * 228 * @return the era applicable at this date, not null 229 */ 230 @Override 231 public MinguoEra getEra() { 232 return (getProlepticYear() >= 1 ? MinguoEra.ROC : MinguoEra.BEFORE_ROC); 233 } 234 235 /** 236 * Returns the length of the month represented by this date. 237 * <p> 238 * This returns the length of the month in days. 239 * Month lengths match those of the ISO calendar system. 240 * 241 * @return the length of the month in days 242 */ 243 @Override 244 public int lengthOfMonth() { 245 return isoDate.lengthOfMonth(); 246 } 247 248 //----------------------------------------------------------------------- 249 @Override 250 public ValueRange range(TemporalField field) { 251 if (field instanceof ChronoField) { 252 if (isSupported(field)) { 253 ChronoField f = (ChronoField) field; 254 switch (f) { 255 case DAY_OF_MONTH: 256 case DAY_OF_YEAR: 257 case ALIGNED_WEEK_OF_MONTH: 258 return isoDate.range(field); 259 case YEAR_OF_ERA: { 260 ValueRange range = YEAR.range(); 261 long max = (getProlepticYear() <= 0 ? -range.getMinimum() + 1 + YEARS_DIFFERENCE : range.getMaximum() - YEARS_DIFFERENCE); 262 return ValueRange.of(1, max); 263 } 264 } 265 return getChronology().range(f); 266 } 267 throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName()); 268 } 269 return field.rangeRefinedBy(this); 270 } 271 272 @Override 273 public long getLong(TemporalField field) { 274 if (field instanceof ChronoField) { 275 switch ((ChronoField) field) { 276 case PROLEPTIC_MONTH: 277 return getProlepticMonth(); 278 case YEAR_OF_ERA: { 279 int prolepticYear = getProlepticYear(); 280 return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); 281 } 282 case YEAR: 283 return getProlepticYear(); 284 case ERA: 285 return (getProlepticYear() >= 1 ? 1 : 0); 286 } 287 return isoDate.getLong(field); 288 } 289 return field.getFrom(this); 290 } 291 292 private long getProlepticMonth() { 293 return getProlepticYear() * 12L + isoDate.getMonthValue() - 1; 294 } 295 296 private int getProlepticYear() { 297 return isoDate.getYear() - YEARS_DIFFERENCE; 298 } 299 300 //----------------------------------------------------------------------- 301 @Override 302 public MinguoDate with(TemporalField field, long newValue) { 303 if (field instanceof ChronoField) { 304 ChronoField f = (ChronoField) field; 305 if (getLong(f) == newValue) { 306 return this; 307 } 308 switch (f) { 309 case PROLEPTIC_MONTH: 310 getChronology().range(f).checkValidValue(newValue, f); 311 return plusMonths(newValue - getProlepticMonth()); 312 case YEAR_OF_ERA: 313 case YEAR: 314 case ERA: { 315 int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); 316 switch (f) { 317 case YEAR_OF_ERA: 318 return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE)); 319 case YEAR: 320 return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE)); 321 case ERA: 322 return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE)); 323 } 324 } 325 } 326 return with(isoDate.with(field, newValue)); 327 } 328 return ChronoLocalDate.super.with(field, newValue); 329 } 330 331 /** 332 * {@inheritDoc} 333 * @throws DateTimeException {@inheritDoc} 334 * @throws ArithmeticException {@inheritDoc} 335 */ 336 @Override 337 public MinguoDate with(TemporalAdjuster adjuster) { 338 return super.with(adjuster); 339 } 340 341 /** 342 * {@inheritDoc} 343 * @throws DateTimeException {@inheritDoc} 344 * @throws ArithmeticException {@inheritDoc} 345 */ 346 @Override 347 public MinguoDate plus(TemporalAmount amount) { 348 return super.plus(amount); 349 } 350 351 /** 352 * {@inheritDoc} 353 * @throws DateTimeException {@inheritDoc} 354 * @throws ArithmeticException {@inheritDoc} 355 */ 356 @Override 357 public MinguoDate minus(TemporalAmount amount) { 358 return super.minus(amount); 359 } 360 361 //----------------------------------------------------------------------- 362 @Override 363 MinguoDate plusYears(long years) { 364 return with(isoDate.plusYears(years)); 365 } 366 367 @Override 368 MinguoDate plusMonths(long months) { 369 return with(isoDate.plusMonths(months)); 370 } 371 372 @Override 373 MinguoDate plusDays(long days) { 374 return with(isoDate.plusDays(days)); 375 } 376 377 @Override 378 public MinguoDate plus(long amountToAdd, TemporalUnit unit) { 379 return super.plus(amountToAdd, unit); 380 } 381 382 @Override 383 public MinguoDate minus(long amountToAdd, TemporalUnit unit) { 384 return super.minus(amountToAdd, unit); 385 } 386 387 @Override 388 MinguoDate plusWeeks(long weeksToAdd) { 389 return super.plusWeeks(weeksToAdd); 390 } 391 392 @Override 393 MinguoDate minusYears(long yearsToSubtract) { 394 return super.minusYears(yearsToSubtract); 395 } 396 397 @Override 398 MinguoDate minusMonths(long monthsToSubtract) { 399 return super.minusMonths(monthsToSubtract); 400 } 401 402 @Override 403 MinguoDate minusWeeks(long weeksToSubtract) { 404 return super.minusWeeks(weeksToSubtract); 405 } 406 407 @Override 408 MinguoDate minusDays(long daysToSubtract) { 409 return super.minusDays(daysToSubtract); 410 } 411 412 private MinguoDate with(LocalDate newDate) { 413 return (newDate.equals(isoDate) ? this : new MinguoDate(newDate)); 414 } 415 416 @Override // for javadoc and covariant return type 417 public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) { 418 return super.atTime(localTime); 419 } 420 421 @Override 422 public Period periodUntil(ChronoLocalDate<?> endDate) { 423 return isoDate.periodUntil(endDate); 424 } 425 426 @Override // override for performance 427 public long toEpochDay() { 428 return isoDate.toEpochDay(); 429 } 430 431 //------------------------------------------------------------------------- 432 @Override // override for performance 433 public boolean equals(Object obj) { 434 if (this == obj) { 435 return true; 436 } 437 if (obj instanceof MinguoDate) { 438 MinguoDate otherDate = (MinguoDate) obj; 439 return this.isoDate.equals(otherDate.isoDate); 440 } 441 return false; 442 } 443 444 @Override // override for performance 445 public int hashCode() { 446 return getChronology().getId().hashCode() ^ isoDate.hashCode(); 447 } 448 449 //----------------------------------------------------------------------- 450 private Object writeReplace() { 451 return new Ser(Ser.MINGUO_DATE_TYPE, this); 452 } 453 454 void writeExternal(DataOutput out) throws IOException { 455 // MinguoChronology is implicit in the MINGUO_DATE_TYPE 456 out.writeInt(get(YEAR)); 457 out.writeByte(get(MONTH_OF_YEAR)); 458 out.writeByte(get(DAY_OF_MONTH)); 459 } 460 461 static ChronoLocalDate<?> readExternal(DataInput in) throws IOException { 462 int year = in.readInt(); 463 int month = in.readByte(); 464 int dayOfMonth = in.readByte(); 465 return MinguoChronology.INSTANCE.date(year, month, dayOfMonth); 466 } 467 468 }