0001: /*
0002: * Copyright 2001-2007 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.time;
0017:
0018: import java.io.IOException;
0019: import java.io.ObjectInputStream;
0020: import java.io.ObjectOutputStream;
0021: import java.io.Serializable;
0022: import java.util.Calendar;
0023: import java.util.Date;
0024: import java.util.Locale;
0025:
0026: import org.joda.time.base.BaseLocal;
0027: import org.joda.time.chrono.ISOChronology;
0028: import org.joda.time.convert.ConverterManager;
0029: import org.joda.time.convert.PartialConverter;
0030: import org.joda.time.field.AbstractReadableInstantFieldProperty;
0031: import org.joda.time.format.DateTimeFormat;
0032: import org.joda.time.format.ISODateTimeFormat;
0033:
0034: /**
0035: * LocalDateTime is an unmodifiable datetime class representing a
0036: * datetime without a time zone.
0037: * <p>
0038: * LocalDateTime implements the {@link ReadablePartial} interface.
0039: * To do this, certain methods focus on key fields Year, MonthOfYear,
0040: * DayOfYear and MillisOfDay.
0041: * However, <b>all</b> fields may in fact be queried.
0042: * <p>
0043: * Internally, LocalDateTime uses a single millisecond-based value to
0044: * represent the local datetime. This value is only used internally and
0045: * is not exposed to applications.
0046: * <p>
0047: * Calculations on LocalDateTime are performed using a {@link Chronology}.
0048: * This chronology will be set internally to be in the UTC time zone
0049: * for all calculations.
0050: *
0051: * <p>Each individual field can be queried in two ways:
0052: * <ul>
0053: * <li><code>getHourOfDay()</code>
0054: * <li><code>hourOfDay().get()</code>
0055: * </ul>
0056: * The second technique also provides access to other useful methods on the
0057: * field:
0058: * <ul>
0059: * <li>numeric value
0060: * <li>text value
0061: * <li>short text value
0062: * <li>maximum/minimum values
0063: * <li>add/subtract
0064: * <li>set
0065: * <li>rounding
0066: * </ul>
0067: *
0068: * <p>
0069: * LocalDateTime is thread-safe and immutable, provided that the Chronology is as well.
0070: * All standard Chronology classes supplied are thread-safe and immutable.
0071: *
0072: * @author Stephen Colebourne
0073: * @since 1.3
0074: */
0075: public final class LocalDateTime extends BaseLocal implements
0076: ReadablePartial, Serializable {
0077:
0078: /** Serialization lock */
0079: private static final long serialVersionUID = -268716875315837168L;
0080:
0081: /** The index of the year field in the field array */
0082: private static final int YEAR = 0;
0083: /** The index of the monthOfYear field in the field array */
0084: private static final int MONTH_OF_YEAR = 1;
0085: /** The index of the dayOfMonth field in the field array */
0086: private static final int DAY_OF_MONTH = 2;
0087: /** The index of the millis field in the field array */
0088: private static final int MILLIS_OF_DAY = 3;
0089:
0090: /** The local millis from 1970-01-01T00:00:00 */
0091: private long iLocalMillis;
0092: /** The chronology to use in UTC */
0093: private Chronology iChronology;
0094:
0095: //-----------------------------------------------------------------------
0096: /**
0097: * Constructs a LocalDateTime from a <code>java.util.Calendar</code>
0098: * using exactly the same field values avoiding any time zone effects.
0099: * <p>
0100: * Each field is queried from the Calendar and assigned to the LocalDateTime.
0101: * This is useful if you have been using the Calendar as a local date,
0102: * ignoing the zone.
0103: * <p>
0104: * This factory method ignores the type of the calendar and always
0105: * creates a LocalDateTime with ISO chronology. It is expected that you
0106: * will only pass in instances of <code>GregorianCalendar</code> however
0107: * this is not validated.
0108: *
0109: * @param calendar the Calendar to extract fields from
0110: * @return the created LocalDateTime
0111: * @throws IllegalArgumentException if the calendar is null
0112: * @throws IllegalArgumentException if the date is invalid for the ISO chronology
0113: */
0114: public static LocalDateTime fromCalendarFields(Calendar calendar) {
0115: if (calendar == null) {
0116: throw new IllegalArgumentException(
0117: "The calendar must not be null");
0118: }
0119: return new LocalDateTime(calendar.get(Calendar.YEAR), calendar
0120: .get(Calendar.MONTH) + 1, calendar
0121: .get(Calendar.DAY_OF_MONTH), calendar
0122: .get(Calendar.HOUR_OF_DAY), calendar
0123: .get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
0124: calendar.get(Calendar.MILLISECOND));
0125: }
0126:
0127: /**
0128: * Constructs a LocalDateTime from a <code>java.util.Date</code>
0129: * using exactly the same field values avoiding any time zone effects.
0130: * <p>
0131: * Each field is queried from the Date and assigned to the LocalDateTime.
0132: * This is useful if you have been using the Date as a local date,
0133: * ignoing the zone.
0134: * <p>
0135: * This factory method always creates a LocalDateTime with ISO chronology.
0136: *
0137: * @param date the Date to extract fields from
0138: * @return the created LocalDateTime
0139: * @throws IllegalArgumentException if the calendar is null
0140: * @throws IllegalArgumentException if the date is invalid for the ISO chronology
0141: */
0142: public static LocalDateTime fromDateFields(Date date) {
0143: if (date == null) {
0144: throw new IllegalArgumentException(
0145: "The date must not be null");
0146: }
0147: return new LocalDateTime(date.getYear() + 1900,
0148: date.getMonth() + 1, date.getDate(), date.getHours(),
0149: date.getMinutes(), date.getSeconds(), (int) (date
0150: .getTime() % 1000));
0151: }
0152:
0153: //-----------------------------------------------------------------------
0154: /**
0155: * Constructs an instance set to the current local time evaluated using
0156: * ISO chronology in the default zone.
0157: * <p>
0158: * Once the constructor is completed, the zone is no longer used.
0159: */
0160: public LocalDateTime() {
0161: this (DateTimeUtils.currentTimeMillis(), ISOChronology
0162: .getInstance());
0163: }
0164:
0165: /**
0166: * Constructs an instance set to the current local time evaluated using
0167: * ISO chronology in the specified zone.
0168: * <p>
0169: * If the specified time zone is null, the default zone is used.
0170: * Once the constructor is completed, the zone is no longer used.
0171: *
0172: * @param zone the time zone, null means default zone
0173: */
0174: public LocalDateTime(DateTimeZone zone) {
0175: this (DateTimeUtils.currentTimeMillis(), ISOChronology
0176: .getInstance(zone));
0177: }
0178:
0179: /**
0180: * Constructs an instance set to the current local time evaluated using
0181: * specified chronology.
0182: * <p>
0183: * If the chronology is null, ISO chronology in the default time zone is used.
0184: * Once the constructor is completed, the zone is no longer used.
0185: *
0186: * @param chronology the chronology, null means ISOChronology in default zone
0187: */
0188: public LocalDateTime(Chronology chronology) {
0189: this (DateTimeUtils.currentTimeMillis(), chronology);
0190: }
0191:
0192: //-----------------------------------------------------------------------
0193: /**
0194: * Constructs an instance set to the local time defined by the specified
0195: * instant evaluated using ISO chronology in the default zone.
0196: * <p>
0197: * Once the constructor is completed, the zone is no longer used.
0198: *
0199: * @param instant the milliseconds from 1970-01-01T00:00:00Z
0200: */
0201: public LocalDateTime(long instant) {
0202: this (instant, ISOChronology.getInstance());
0203: }
0204:
0205: /**
0206: * Constructs an instance set to the local time defined by the specified
0207: * instant evaluated using ISO chronology in the specified zone.
0208: * <p>
0209: * If the specified time zone is null, the default zone is used.
0210: * Once the constructor is completed, the zone is no longer used.
0211: *
0212: * @param instant the milliseconds from 1970-01-01T00:00:00Z
0213: * @param zone the time zone, null means default zone
0214: */
0215: public LocalDateTime(long instant, DateTimeZone zone) {
0216: this (instant, ISOChronology.getInstance(zone));
0217: }
0218:
0219: /**
0220: * Constructs an instance set to the local time defined by the specified
0221: * instant evaluated using the specified chronology.
0222: * <p>
0223: * If the chronology is null, ISO chronology in the default zone is used.
0224: * Once the constructor is completed, the zone is no longer used.
0225: *
0226: * @param instant the milliseconds from 1970-01-01T00:00:00Z
0227: * @param chronology the chronology, null means ISOChronology in default zone
0228: */
0229: public LocalDateTime(long instant, Chronology chronology) {
0230: chronology = DateTimeUtils.getChronology(chronology);
0231:
0232: long localMillis = chronology.getZone().getMillisKeepLocal(
0233: DateTimeZone.UTC, instant);
0234: iLocalMillis = localMillis;
0235: iChronology = chronology.withUTC();
0236: }
0237:
0238: //-----------------------------------------------------------------------
0239: /**
0240: * Constructs an instance from an Object that represents a datetime.
0241: * <p>
0242: * If the object contains no chronology, <code>ISOChronology</code> is used.
0243: * If the object contains no time zone, the default zone is used.
0244: * Once the constructor is completed, the zone is no longer used.
0245: * <p>
0246: * The recognised object types are defined in
0247: * {@link org.joda.time.convert.ConverterManager ConverterManager} and
0248: * include ReadablePartial, ReadableInstant, String, Calendar and Date.
0249: * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}.
0250: * The default String converter ignores the zone and only parses the field values.
0251: *
0252: * @param instant the datetime object
0253: * @throws IllegalArgumentException if the instant is invalid
0254: */
0255: public LocalDateTime(Object instant) {
0256: this (instant, (Chronology) null);
0257: }
0258:
0259: /**
0260: * Constructs an instance from an Object that represents a datetime,
0261: * forcing the time zone to that specified.
0262: * <p>
0263: * If the object contains no chronology, <code>ISOChronology</code> is used.
0264: * If the specified time zone is null, the default zone is used.
0265: * Once the constructor is completed, the zone is no longer used.
0266: * <p>
0267: * The recognised object types are defined in
0268: * {@link org.joda.time.convert.ConverterManager ConverterManager} and
0269: * include ReadablePartial, ReadableInstant, String, Calendar and Date.
0270: * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}.
0271: * The default String converter ignores the zone and only parses the field values.
0272: *
0273: * @param instant the datetime object
0274: * @param zone the time zone
0275: * @throws IllegalArgumentException if the instant is invalid
0276: */
0277: public LocalDateTime(Object instant, DateTimeZone zone) {
0278: PartialConverter converter = ConverterManager.getInstance()
0279: .getPartialConverter(instant);
0280: Chronology chronology = converter.getChronology(instant, zone);
0281: chronology = DateTimeUtils.getChronology(chronology);
0282: iChronology = chronology.withUTC();
0283: int[] values = converter.getPartialValues(this , instant,
0284: chronology, ISODateTimeFormat
0285: .localDateOptionalTimeParser());
0286: iLocalMillis = iChronology.getDateTimeMillis(values[0],
0287: values[1], values[2], values[3]);
0288: }
0289:
0290: /**
0291: * Constructs an instance from an Object that represents a datetime,
0292: * using the specified chronology.
0293: * <p>
0294: * If the chronology is null, ISO in the default time zone is used.
0295: * Once the constructor is completed, the zone is no longer used.
0296: * <p>
0297: * The recognised object types are defined in
0298: * {@link org.joda.time.convert.ConverterManager ConverterManager} and
0299: * include ReadablePartial, ReadableInstant, String, Calendar and Date.
0300: * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}.
0301: * The default String converter ignores the zone and only parses the field values.
0302: *
0303: * @param instant the datetime object
0304: * @param chronology the chronology
0305: * @throws IllegalArgumentException if the instant is invalid
0306: */
0307: public LocalDateTime(Object instant, Chronology chronology) {
0308: PartialConverter converter = ConverterManager.getInstance()
0309: .getPartialConverter(instant);
0310: chronology = converter.getChronology(instant, chronology);
0311: chronology = DateTimeUtils.getChronology(chronology);
0312: iChronology = chronology.withUTC();
0313: int[] values = converter.getPartialValues(this , instant,
0314: chronology, ISODateTimeFormat
0315: .localDateOptionalTimeParser());
0316: iLocalMillis = iChronology.getDateTimeMillis(values[0],
0317: values[1], values[2], values[3]);
0318: }
0319:
0320: //-----------------------------------------------------------------------
0321: /**
0322: * Constructs an instance set to the specified date and time
0323: * using <code>ISOChronology</code>.
0324: *
0325: * @param year the year
0326: * @param monthOfYear the month of the year
0327: * @param dayOfMonth the day of the month
0328: * @param hourOfDay the hour of the day
0329: * @param minuteOfHour the minute of the hour
0330: */
0331: public LocalDateTime(int year, int monthOfYear, int dayOfMonth,
0332: int hourOfDay, int minuteOfHour) {
0333: this (year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, 0,
0334: 0, ISOChronology.getInstanceUTC());
0335: }
0336:
0337: /**
0338: * Constructs an instance set to the specified date and time
0339: * using <code>ISOChronology</code>.
0340: *
0341: * @param year the year
0342: * @param monthOfYear the month of the year
0343: * @param dayOfMonth the day of the month
0344: * @param hourOfDay the hour of the day
0345: * @param minuteOfHour the minute of the hour
0346: * @param secondOfMinute the second of the minute
0347: */
0348: public LocalDateTime(int year, int monthOfYear, int dayOfMonth,
0349: int hourOfDay, int minuteOfHour, int secondOfMinute) {
0350: this (year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour,
0351: secondOfMinute, 0, ISOChronology.getInstanceUTC());
0352: }
0353:
0354: /**
0355: * Constructs an instance set to the specified date and time
0356: * using <code>ISOChronology</code>.
0357: *
0358: * @param year the year
0359: * @param monthOfYear the month of the year
0360: * @param dayOfMonth the day of the month
0361: * @param hourOfDay the hour of the day
0362: * @param minuteOfHour the minute of the hour
0363: * @param secondOfMinute the second of the minute
0364: * @param millisOfSecond the millisecond of the second
0365: */
0366: public LocalDateTime(int year, int monthOfYear, int dayOfMonth,
0367: int hourOfDay, int minuteOfHour, int secondOfMinute,
0368: int millisOfSecond) {
0369: this (year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour,
0370: secondOfMinute, millisOfSecond, ISOChronology
0371: .getInstanceUTC());
0372: }
0373:
0374: /**
0375: * Constructs an instance set to the specified date and time
0376: * using the specified chronology, whose zone is ignored.
0377: * <p>
0378: * If the chronology is null, <code>ISOChronology</code> is used.
0379: *
0380: * @param year the year
0381: * @param monthOfYear the month of the year
0382: * @param dayOfMonth the day of the month
0383: * @param hourOfDay the hour of the day
0384: * @param minuteOfHour the minute of the hour
0385: * @param secondOfMinute the second of the minute
0386: * @param millisOfSecond the millisecond of the second
0387: * @param chronology the chronology, null means ISOChronology in default zone
0388: */
0389: public LocalDateTime(int year, int monthOfYear, int dayOfMonth,
0390: int hourOfDay, int minuteOfHour, int secondOfMinute,
0391: int millisOfSecond, Chronology chronology) {
0392: super ();
0393: chronology = DateTimeUtils.getChronology(chronology).withUTC();
0394: long instant = chronology.getDateTimeMillis(year, monthOfYear,
0395: dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute,
0396: millisOfSecond);
0397: iChronology = chronology;
0398: iLocalMillis = instant;
0399: }
0400:
0401: //-----------------------------------------------------------------------
0402: /**
0403: * Gets the number of fields in this partial, which is four.
0404: * The supported fields are Year, MonthOfDay, DayOfMonth and MillisOfDay.
0405: *
0406: * @return the field count, four
0407: */
0408: public int size() {
0409: return 4;
0410: }
0411:
0412: /**
0413: * Gets the field for a specific index in the chronology specified.
0414: * <p>
0415: * This method must not use any instance variables.
0416: *
0417: * @param index the index to retrieve
0418: * @param chrono the chronology to use
0419: * @return the field
0420: */
0421: protected DateTimeField getField(int index, Chronology chrono) {
0422: switch (index) {
0423: case YEAR:
0424: return chrono.year();
0425: case MONTH_OF_YEAR:
0426: return chrono.monthOfYear();
0427: case DAY_OF_MONTH:
0428: return chrono.dayOfMonth();
0429: case MILLIS_OF_DAY:
0430: return chrono.millisOfDay();
0431: default:
0432: throw new IndexOutOfBoundsException("Invalid index: "
0433: + index);
0434: }
0435: }
0436:
0437: /**
0438: * Gets the value of the field at the specifed index.
0439: * <p>
0440: * This method is required to support the <code>ReadablePartial</code>
0441: * interface. The supported fields are Year, MonthOfDay, DayOfMonth and MillisOfDay.
0442: *
0443: * @param index the index, zero to two
0444: * @return the value
0445: * @throws IndexOutOfBoundsException if the index is invalid
0446: */
0447: public int getValue(int index) {
0448: switch (index) {
0449: case YEAR:
0450: return getChronology().year().get(getLocalMillis());
0451: case MONTH_OF_YEAR:
0452: return getChronology().monthOfYear().get(getLocalMillis());
0453: case DAY_OF_MONTH:
0454: return getChronology().dayOfMonth().get(getLocalMillis());
0455: case MILLIS_OF_DAY:
0456: return getChronology().millisOfDay().get(getLocalMillis());
0457: default:
0458: throw new IndexOutOfBoundsException("Invalid index: "
0459: + index);
0460: }
0461: }
0462:
0463: //-----------------------------------------------------------------------
0464: /**
0465: * Get the value of one of the fields of a datetime.
0466: * <p>
0467: * This method gets the value of the specified field.
0468: * For example:
0469: * <pre>
0470: * DateTime dt = new DateTime();
0471: * int year = dt.get(DateTimeFieldType.year());
0472: * </pre>
0473: *
0474: * @param type a field type, usually obtained from DateTimeFieldType, not null
0475: * @return the value of that field
0476: * @throws IllegalArgumentException if the field type is null
0477: */
0478: public int get(DateTimeFieldType type) {
0479: if (type == null) {
0480: throw new IllegalArgumentException(
0481: "The DateTimeFieldType must not be null");
0482: }
0483: return type.getField(getChronology()).get(getLocalMillis());
0484: }
0485:
0486: /**
0487: * Checks if the field type specified is supported by this
0488: * local datetime and chronology.
0489: * This can be used to avoid exceptions in {@link #get(DateTimeFieldType)}.
0490: *
0491: * @param type a field type, usually obtained from DateTimeFieldType
0492: * @return true if the field type is supported
0493: */
0494: public boolean isSupported(DateTimeFieldType type) {
0495: if (type == null) {
0496: return false;
0497: }
0498: return type.getField(getChronology()).isSupported();
0499: }
0500:
0501: /**
0502: * Checks if the duration type specified is supported by this
0503: * local datetime and chronology.
0504: *
0505: * @param type a duration type, usually obtained from DurationFieldType
0506: * @return true if the field type is supported
0507: */
0508: public boolean isSupported(DurationFieldType type) {
0509: if (type == null) {
0510: return false;
0511: }
0512: return type.getField(getChronology()).isSupported();
0513: }
0514:
0515: //-----------------------------------------------------------------------
0516: /**
0517: * Gets the milliseconds of the datetime instant from the Java epoch
0518: * of 1970-01-01T00:00:00 (not fixed to any specific time zone).
0519: *
0520: * @return the number of milliseconds since 1970-01-01T00:00:00
0521: * @since 1.5 (previously private)
0522: */
0523: protected long getLocalMillis() {
0524: return iLocalMillis;
0525: }
0526:
0527: /**
0528: * Gets the chronology of the datetime.
0529: *
0530: * @return the Chronology that the datetime is using
0531: */
0532: public Chronology getChronology() {
0533: return iChronology;
0534: }
0535:
0536: //-----------------------------------------------------------------------
0537: /**
0538: * Compares this ReadablePartial with another returning true if the chronology,
0539: * field types and values are equal.
0540: *
0541: * @param partial an object to check against
0542: * @return true if fields and values are equal
0543: */
0544: public boolean equals(Object partial) {
0545: // override to perform faster
0546: if (this == partial) {
0547: return true;
0548: }
0549: if (partial instanceof LocalDateTime) {
0550: LocalDateTime other = (LocalDateTime) partial;
0551: if (iChronology.equals(other.iChronology)) {
0552: return iLocalMillis == other.iLocalMillis;
0553: }
0554: }
0555: return super .equals(partial);
0556: }
0557:
0558: /**
0559: * Compares this partial with another returning an integer
0560: * indicating the order.
0561: * <p>
0562: * The fields are compared in order, from largest to smallest.
0563: * The first field that is non-equal is used to determine the result.
0564: * <p>
0565: * The specified object must be a partial instance whose field types
0566: * match those of this partial.
0567: * <p>
0568: * NOTE: This implementation violates the Comparable contract.
0569: * This method will accept any instance of ReadablePartial as input.
0570: * However, it is possible that some implementations of ReadablePartial
0571: * exist that do not extend AbstractPartial, and thus will throw a
0572: * ClassCastException if compared in the opposite direction.
0573: * The cause of this problem is that ReadablePartial doesn't define
0574: * the compareTo() method, however we can't change that until v2.0.
0575: *
0576: * @param partial an object to check against
0577: * @return negative if this is less, zero if equal, positive if greater
0578: * @throws ClassCastException if the partial is the wrong class
0579: * or if it has field types that don't match
0580: * @throws NullPointerException if the partial is null
0581: */
0582: public int compareTo(Object partial) {
0583: // override to perform faster
0584: if (this == partial) {
0585: return 0;
0586: }
0587: if (partial instanceof LocalDateTime) {
0588: LocalDateTime other = (LocalDateTime) partial;
0589: if (iChronology.equals(other.iChronology)) {
0590: return (iLocalMillis < other.iLocalMillis ? -1
0591: : (iLocalMillis == other.iLocalMillis ? 0 : 1));
0592:
0593: }
0594: }
0595: return super .compareTo(partial);
0596: }
0597:
0598: //-----------------------------------------------------------------------
0599: /**
0600: * Converts this object to a DateTime using the default zone.
0601: * <p>
0602: * This method will throw an exception if the datetime that would be
0603: * created does not exist when the time zone is taken into account.
0604: *
0605: * @return <code>this</code>
0606: */
0607: public DateTime toDateTime() {
0608: return toDateTime((DateTimeZone) null);
0609: }
0610:
0611: /**
0612: * Converts this object to a DateTime using the specified zone.
0613: * <p>
0614: * This method will throw an exception if the datetime that would be
0615: * created does not exist when the time zone is taken into account.
0616: *
0617: * @param zone time zone to apply, or default if null
0618: * @return a DateTime using the same millis
0619: */
0620: public DateTime toDateTime(DateTimeZone zone) {
0621: zone = DateTimeUtils.getZone(zone);
0622: Chronology chrono = iChronology.withZone(zone);
0623: return new DateTime(getYear(), getMonthOfYear(),
0624: getDayOfMonth(), getHourOfDay(), getMinuteOfHour(),
0625: getSecondOfMinute(), getMillisOfSecond(), chrono);
0626: }
0627:
0628: //-----------------------------------------------------------------------
0629: /**
0630: * Converts this object to a LocalDate with the same date and chronology.
0631: *
0632: * @return a LocalDate with the same date and chronology
0633: */
0634: public LocalDate toLocalDate() {
0635: return new LocalDate(getLocalMillis(), getChronology());
0636: }
0637:
0638: /**
0639: * Converts this object to a LocalTime with the same time and chronology.
0640: *
0641: * @return a LocalTime with the same time and chronology
0642: */
0643: public LocalTime toLocalTime() {
0644: return new LocalTime(getLocalMillis(), getChronology());
0645: }
0646:
0647: //-----------------------------------------------------------------------
0648: /**
0649: * Returns a copy of this datetime with different local millis.
0650: * <p>
0651: * The returned object will be a new instance of the same type.
0652: * Only the millis will change, the chronology is kept.
0653: * The returned object will be either be a new instance or <code>this</code>.
0654: *
0655: * @param newMillis the new millis, from 1970-01-01T00:00:00
0656: * @return a copy of this datetime with different millis
0657: */
0658: LocalDateTime withLocalMillis(long newMillis) {
0659: return (newMillis == getLocalMillis() ? this
0660: : new LocalDateTime(newMillis, getChronology()));
0661: }
0662:
0663: //-----------------------------------------------------------------------
0664: /**
0665: * Returns a copy of this datetime with the specified date,
0666: * retaining the time fields.
0667: * <p>
0668: * If the date is already the date passed in, then <code>this</code> is returned.
0669: * <p>
0670: * To set a single field use the properties, for example:
0671: * <pre>
0672: * DateTime set = dt.monthOfYear().setCopy(6);
0673: * </pre>
0674: *
0675: * @param year the new year value
0676: * @param monthOfYear the new monthOfYear value
0677: * @param dayOfMonth the new dayOfMonth value
0678: * @return a copy of this datetime with a different date
0679: * @throws IllegalArgumentException if any value if invalid
0680: */
0681: public LocalDateTime withDate(int year, int monthOfYear,
0682: int dayOfMonth) {
0683: Chronology chrono = getChronology();
0684: long instant = getLocalMillis();
0685: instant = chrono.year().set(instant, year);
0686: instant = chrono.monthOfYear().set(instant, monthOfYear);
0687: instant = chrono.dayOfMonth().set(instant, dayOfMonth);
0688: return withLocalMillis(instant);
0689: }
0690:
0691: /**
0692: * Returns a copy of this datetime with the specified time,
0693: * retaining the date fields.
0694: * <p>
0695: * If the time is already the time passed in, then <code>this</code> is returned.
0696: * <p>
0697: * To set a single field use the properties, for example:
0698: * <pre>
0699: * LocalDateTime set = dt.hourOfDay().setCopy(6);
0700: * </pre>
0701: *
0702: * @param hourOfDay the hour of the day
0703: * @param minuteOfHour the minute of the hour
0704: * @param secondOfMinute the second of the minute
0705: * @param millisOfSecond the millisecond of the second
0706: * @return a copy of this datetime with a different time
0707: * @throws IllegalArgumentException if any value if invalid
0708: */
0709: public LocalDateTime withTime(int hourOfDay, int minuteOfHour,
0710: int secondOfMinute, int millisOfSecond) {
0711: Chronology chrono = getChronology();
0712: long instant = getLocalMillis();
0713: instant = chrono.hourOfDay().set(instant, hourOfDay);
0714: instant = chrono.minuteOfHour().set(instant, minuteOfHour);
0715: instant = chrono.secondOfMinute().set(instant, secondOfMinute);
0716: instant = chrono.millisOfSecond().set(instant, millisOfSecond);
0717: return withLocalMillis(instant);
0718: }
0719:
0720: //-----------------------------------------------------------------------
0721: /**
0722: * Returns a copy of this datetime with the partial set of fields
0723: * replacing those from this instance.
0724: * <p>
0725: * For example, if the partial is a <code>TimeOfDay</code> then the time fields
0726: * would be changed in the returned instance.
0727: * If the partial is null, then <code>this</code> is returned.
0728: *
0729: * @param partial the partial set of fields to apply to this datetime, null ignored
0730: * @return a copy of this datetime with a different set of fields
0731: * @throws IllegalArgumentException if any value is invalid
0732: */
0733: public LocalDateTime withFields(ReadablePartial partial) {
0734: if (partial == null) {
0735: return this ;
0736: }
0737: return withLocalMillis(getChronology().set(partial,
0738: getLocalMillis()));
0739: }
0740:
0741: /**
0742: * Returns a copy of this datetime with the specified field set to a new value.
0743: * <p>
0744: * For example, if the field type is <code>hourOfDay</code> then the hour of day
0745: * field would be changed in the returned instance.
0746: * If the field type is null, then <code>this</code> is returned.
0747: * <p>
0748: * These three lines are equivalent:
0749: * <pre>
0750: * LocalDateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6);
0751: * LocalDateTime updated = dt.dayOfMonth().setCopy(6);
0752: * LocalDateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
0753: * </pre>
0754: *
0755: * @param fieldType the field type to set, not null
0756: * @param value the value to set
0757: * @return a copy of this datetime with the field set
0758: * @throws IllegalArgumentException if the value is null or invalid
0759: */
0760: public LocalDateTime withField(DateTimeFieldType fieldType,
0761: int value) {
0762: if (fieldType == null) {
0763: throw new IllegalArgumentException("Field must not be null");
0764: }
0765: long instant = fieldType.getField(getChronology()).set(
0766: getLocalMillis(), value);
0767: return withLocalMillis(instant);
0768: }
0769:
0770: /**
0771: * Returns a copy of this datetime with the value of the specified
0772: * field increased.
0773: * <p>
0774: * If the addition is zero or the field is null, then <code>this</code> is returned.
0775: * <p>
0776: * These three lines are equivalent:
0777: * <pre>
0778: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
0779: * LocalDateTime added = dt.plusYears(6);
0780: * LocalDateTime added = dt.plus(Period.years(6));
0781: * </pre>
0782: *
0783: * @param fieldType the field type to add to, not null
0784: * @param amount the amount to add
0785: * @return a copy of this datetime with the field updated
0786: * @throws IllegalArgumentException if the value is null or invalid
0787: * @throws ArithmeticException if the result exceeds the internal capacity
0788: */
0789: public LocalDateTime withFieldAdded(DurationFieldType fieldType,
0790: int amount) {
0791: if (fieldType == null) {
0792: throw new IllegalArgumentException("Field must not be null");
0793: }
0794: if (amount == 0) {
0795: return this ;
0796: }
0797: long instant = fieldType.getField(getChronology()).add(
0798: getLocalMillis(), amount);
0799: return withLocalMillis(instant);
0800: }
0801:
0802: //-----------------------------------------------------------------------
0803: /**
0804: * Returns a copy of this datetime with the specified duration added.
0805: * <p>
0806: * If the addition is zero, then <code>this</code> is returned.
0807: *
0808: * @param durationToAdd the duration to add to this one, null means zero
0809: * @param scalar the amount of times to add, such as -1 to subtract once
0810: * @return a copy of this datetime with the duration added
0811: * @throws ArithmeticException if the result exceeds the internal capacity
0812: */
0813: public LocalDateTime withDurationAdded(
0814: ReadableDuration durationToAdd, int scalar) {
0815: if (durationToAdd == null || scalar == 0) {
0816: return this ;
0817: }
0818: long instant = getChronology().add(getLocalMillis(),
0819: durationToAdd.getMillis(), scalar);
0820: return withLocalMillis(instant);
0821: }
0822:
0823: /**
0824: * Returns a copy of this datetime with the specified period added.
0825: * <p>
0826: * If the addition is zero, then <code>this</code> is returned.
0827: * <p>
0828: * This method is typically used to add multiple copies of complex
0829: * period instances. Adding one field is best achieved using methods
0830: * like {@link #withFieldAdded(DurationFieldType, int)}
0831: * or {@link #plusYears(int)}.
0832: *
0833: * @param period the period to add to this one, null means zero
0834: * @param scalar the amount of times to add, such as -1 to subtract once
0835: * @return a copy of this datetime with the period added
0836: * @throws ArithmeticException if the result exceeds the internal capacity
0837: */
0838: public LocalDateTime withPeriodAdded(ReadablePeriod period,
0839: int scalar) {
0840: if (period == null || scalar == 0) {
0841: return this ;
0842: }
0843: long instant = getChronology().add(period, getLocalMillis(),
0844: scalar);
0845: return withLocalMillis(instant);
0846: }
0847:
0848: //-----------------------------------------------------------------------
0849: /**
0850: * Returns a copy of this datetime with the specified duration added.
0851: * <p>
0852: * If the amount is zero or null, then <code>this</code> is returned.
0853: *
0854: * @param duration the duration to add to this one, null means zero
0855: * @return a copy of this datetime with the duration added
0856: * @throws ArithmeticException if the result exceeds the internal capacity
0857: */
0858: public LocalDateTime plus(ReadableDuration duration) {
0859: return withDurationAdded(duration, 1);
0860: }
0861:
0862: /**
0863: * Returns a copy of this datetime with the specified period added.
0864: * <p>
0865: * If the amount is zero or null, then <code>this</code> is returned.
0866: * <p>
0867: * This method is typically used to add complex period instances.
0868: * Adding one field is best achieved using methods
0869: * like {@link #plusYears(int)}.
0870: *
0871: * @param period the period to add to this one, null means zero
0872: * @return a copy of this datetime with the period added
0873: * @throws ArithmeticException if the result exceeds the internal capacity
0874: */
0875: public LocalDateTime plus(ReadablePeriod period) {
0876: return withPeriodAdded(period, 1);
0877: }
0878:
0879: //-----------------------------------------------------------------------
0880: /**
0881: * Returns a copy of this datetime plus the specified number of years.
0882: * <p>
0883: * This LocalDateTime instance is immutable and unaffected by this method call.
0884: * <p>
0885: * The following three lines are identical in effect:
0886: * <pre>
0887: * LocalDateTime added = dt.plusYears(6);
0888: * LocalDateTime added = dt.plus(Period.years(6));
0889: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
0890: * </pre>
0891: *
0892: * @param years the amount of years to add, may be negative
0893: * @return the new LocalDateTime plus the increased years
0894: */
0895: public LocalDateTime plusYears(int years) {
0896: if (years == 0) {
0897: return this ;
0898: }
0899: long instant = getChronology().years().add(getLocalMillis(),
0900: years);
0901: return withLocalMillis(instant);
0902: }
0903:
0904: /**
0905: * Returns a copy of this datetime plus the specified number of months.
0906: * <p>
0907: * This LocalDateTime instance is immutable and unaffected by this method call.
0908: * <p>
0909: * The following three lines are identical in effect:
0910: * <pre>
0911: * LocalDateTime added = dt.plusMonths(6);
0912: * LocalDateTime added = dt.plus(Period.months(6));
0913: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);
0914: * </pre>
0915: *
0916: * @param months the amount of months to add, may be negative
0917: * @return the new LocalDateTime plus the increased months
0918: */
0919: public LocalDateTime plusMonths(int months) {
0920: if (months == 0) {
0921: return this ;
0922: }
0923: long instant = getChronology().months().add(getLocalMillis(),
0924: months);
0925: return withLocalMillis(instant);
0926: }
0927:
0928: /**
0929: * Returns a copy of this datetime plus the specified number of weeks.
0930: * <p>
0931: * This LocalDateTime instance is immutable and unaffected by this method call.
0932: * <p>
0933: * The following three lines are identical in effect:
0934: * <pre>
0935: * LocalDateTime added = dt.plusWeeks(6);
0936: * LocalDateTime added = dt.plus(Period.weeks(6));
0937: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.weeks(), 6);
0938: * </pre>
0939: *
0940: * @param weeks the amount of weeks to add, may be negative
0941: * @return the new LocalDateTime plus the increased weeks
0942: */
0943: public LocalDateTime plusWeeks(int weeks) {
0944: if (weeks == 0) {
0945: return this ;
0946: }
0947: long instant = getChronology().weeks().add(getLocalMillis(),
0948: weeks);
0949: return withLocalMillis(instant);
0950: }
0951:
0952: /**
0953: * Returns a copy of this datetime plus the specified number of days.
0954: * <p>
0955: * This LocalDateTime instance is immutable and unaffected by this method call.
0956: * <p>
0957: * The following three lines are identical in effect:
0958: * <pre>
0959: * LocalDateTime added = dt.plusDays(6);
0960: * LocalDateTime added = dt.plus(Period.days(6));
0961: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.days(), 6);
0962: * </pre>
0963: *
0964: * @param days the amount of days to add, may be negative
0965: * @return the new LocalDateTime plus the increased days
0966: */
0967: public LocalDateTime plusDays(int days) {
0968: if (days == 0) {
0969: return this ;
0970: }
0971: long instant = getChronology().days().add(getLocalMillis(),
0972: days);
0973: return withLocalMillis(instant);
0974: }
0975:
0976: //-----------------------------------------------------------------------
0977: /**
0978: * Returns a copy of this datetime plus the specified number of hours.
0979: * <p>
0980: * This LocalDateTime instance is immutable and unaffected by this method call.
0981: * <p>
0982: * The following three lines are identical in effect:
0983: * <pre>
0984: * LocalDateTime added = dt.plusHours(6);
0985: * LocalDateTime added = dt.plus(Period.hours(6));
0986: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6);
0987: * </pre>
0988: *
0989: * @param hours the amount of hours to add, may be negative
0990: * @return the new LocalDateTime plus the increased hours
0991: */
0992: public LocalDateTime plusHours(int hours) {
0993: if (hours == 0) {
0994: return this ;
0995: }
0996: long instant = getChronology().hours().add(getLocalMillis(),
0997: hours);
0998: return withLocalMillis(instant);
0999: }
1000:
1001: /**
1002: * Returns a copy of this datetime plus the specified number of minutes.
1003: * <p>
1004: * This LocalDateTime instance is immutable and unaffected by this method call.
1005: * <p>
1006: * The following three lines are identical in effect:
1007: * <pre>
1008: * LocalDateTime added = dt.plusMinutes(6);
1009: * LocalDateTime added = dt.plus(Period.minutes(6));
1010: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.minutes(), 6);
1011: * </pre>
1012: *
1013: * @param minutes the amount of minutes to add, may be negative
1014: * @return the new LocalDateTime plus the increased minutes
1015: */
1016: public LocalDateTime plusMinutes(int minutes) {
1017: if (minutes == 0) {
1018: return this ;
1019: }
1020: long instant = getChronology().minutes().add(getLocalMillis(),
1021: minutes);
1022: return withLocalMillis(instant);
1023: }
1024:
1025: /**
1026: * Returns a copy of this datetime plus the specified number of seconds.
1027: * <p>
1028: * This LocalDateTime instance is immutable and unaffected by this method call.
1029: * <p>
1030: * The following three lines are identical in effect:
1031: * <pre>
1032: * LocalDateTime added = dt.plusSeconds(6);
1033: * LocalDateTime added = dt.plus(Period.seconds(6));
1034: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.seconds(), 6);
1035: * </pre>
1036: *
1037: * @param seconds the amount of seconds to add, may be negative
1038: * @return the new LocalDateTime plus the increased seconds
1039: */
1040: public LocalDateTime plusSeconds(int seconds) {
1041: if (seconds == 0) {
1042: return this ;
1043: }
1044: long instant = getChronology().seconds().add(getLocalMillis(),
1045: seconds);
1046: return withLocalMillis(instant);
1047: }
1048:
1049: /**
1050: * Returns a copy of this datetime plus the specified number of millis.
1051: * <p>
1052: * This LocalDateTime instance is immutable and unaffected by this method call.
1053: * <p>
1054: * The following three lines are identical in effect:
1055: * <pre>
1056: * LocalDateTime added = dt.plusMillis(6);
1057: * LocalDateTime added = dt.plus(Period.millis(6));
1058: * LocalDateTime added = dt.withFieldAdded(DurationFieldType.millis(), 6);
1059: * </pre>
1060: *
1061: * @param millis the amount of millis to add, may be negative
1062: * @return the new LocalDateTime plus the increased millis
1063: */
1064: public LocalDateTime plusMillis(int millis) {
1065: if (millis == 0) {
1066: return this ;
1067: }
1068: long instant = getChronology().millis().add(getLocalMillis(),
1069: millis);
1070: return withLocalMillis(instant);
1071: }
1072:
1073: //-----------------------------------------------------------------------
1074: /**
1075: * Returns a copy of this datetime with the specified duration taken away.
1076: * <p>
1077: * If the amount is zero or null, then <code>this</code> is returned.
1078: *
1079: * @param duration the duration to reduce this instant by
1080: * @return a copy of this datetime with the duration taken away
1081: * @throws ArithmeticException if the result exceeds the internal capacity
1082: */
1083: public LocalDateTime minus(ReadableDuration duration) {
1084: return withDurationAdded(duration, -1);
1085: }
1086:
1087: /**
1088: * Returns a copy of this datetime with the specified period taken away.
1089: * <p>
1090: * If the amount is zero or null, then <code>this</code> is returned.
1091: * <p>
1092: * This method is typically used to subtract complex period instances.
1093: * Subtracting one field is best achieved using methods
1094: * like {@link #minusYears(int)}.
1095: *
1096: * @param period the period to reduce this instant by
1097: * @return a copy of this datetime with the period taken away
1098: * @throws ArithmeticException if the result exceeds the internal capacity
1099: */
1100: public LocalDateTime minus(ReadablePeriod period) {
1101: return withPeriodAdded(period, -1);
1102: }
1103:
1104: //-----------------------------------------------------------------------
1105: /**
1106: * Returns a copy of this datetime minus the specified number of years.
1107: * <p>
1108: * This LocalDateTime instance is immutable and unaffected by this method call.
1109: * <p>
1110: * The following three lines are identical in effect:
1111: * <pre>
1112: * LocalDateTime subtracted = dt.minusYears(6);
1113: * LocalDateTime subtracted = dt.minus(Period.years(6));
1114: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.years(), -6);
1115: * </pre>
1116: *
1117: * @param years the amount of years to subtract, may be negative
1118: * @return the new LocalDateTime minus the increased years
1119: */
1120: public LocalDateTime minusYears(int years) {
1121: if (years == 0) {
1122: return this ;
1123: }
1124: long instant = getChronology().years().subtract(
1125: getLocalMillis(), years);
1126: return withLocalMillis(instant);
1127: }
1128:
1129: /**
1130: * Returns a copy of this datetime minus the specified number of months.
1131: * <p>
1132: * This LocalDateTime instance is immutable and unaffected by this method call.
1133: * <p>
1134: * The following three lines are identical in effect:
1135: * <pre>
1136: * LocalDateTime subtracted = dt.minusMonths(6);
1137: * LocalDateTime subtracted = dt.minus(Period.months(6));
1138: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.months(), -6);
1139: * </pre>
1140: *
1141: * @param months the amount of months to subtract, may be negative
1142: * @return the new LocalDateTime minus the increased months
1143: */
1144: public LocalDateTime minusMonths(int months) {
1145: if (months == 0) {
1146: return this ;
1147: }
1148: long instant = getChronology().months().subtract(
1149: getLocalMillis(), months);
1150: return withLocalMillis(instant);
1151: }
1152:
1153: /**
1154: * Returns a copy of this datetime minus the specified number of weeks.
1155: * <p>
1156: * This LocalDateTime instance is immutable and unaffected by this method call.
1157: * <p>
1158: * The following three lines are identical in effect:
1159: * <pre>
1160: * LocalDateTime subtracted = dt.minusWeeks(6);
1161: * LocalDateTime subtracted = dt.minus(Period.weeks(6));
1162: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.weeks(), -6);
1163: * </pre>
1164: *
1165: * @param weeks the amount of weeks to subtract, may be negative
1166: * @return the new LocalDateTime minus the increased weeks
1167: */
1168: public LocalDateTime minusWeeks(int weeks) {
1169: if (weeks == 0) {
1170: return this ;
1171: }
1172: long instant = getChronology().weeks().subtract(
1173: getLocalMillis(), weeks);
1174: return withLocalMillis(instant);
1175: }
1176:
1177: /**
1178: * Returns a copy of this datetime minus the specified number of days.
1179: * <p>
1180: * This LocalDateTime instance is immutable and unaffected by this method call.
1181: * <p>
1182: * The following three lines are identical in effect:
1183: * <pre>
1184: * LocalDateTime subtracted = dt.minusDays(6);
1185: * LocalDateTime subtracted = dt.minus(Period.days(6));
1186: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6);
1187: * </pre>
1188: *
1189: * @param days the amount of days to subtract, may be negative
1190: * @return the new LocalDateTime minus the increased days
1191: */
1192: public LocalDateTime minusDays(int days) {
1193: if (days == 0) {
1194: return this ;
1195: }
1196: long instant = getChronology().days().subtract(
1197: getLocalMillis(), days);
1198: return withLocalMillis(instant);
1199: }
1200:
1201: //-----------------------------------------------------------------------
1202: /**
1203: * Returns a copy of this datetime minus the specified number of hours.
1204: * <p>
1205: * This LocalDateTime instance is immutable and unaffected by this method call.
1206: * <p>
1207: * The following three lines are identical in effect:
1208: * <pre>
1209: * LocalDateTime subtracted = dt.minusHours(6);
1210: * LocalDateTime subtracted = dt.minus(Period.hours(6));
1211: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);
1212: * </pre>
1213: *
1214: * @param hours the amount of hours to subtract, may be negative
1215: * @return the new LocalDateTime minus the increased hours
1216: */
1217: public LocalDateTime minusHours(int hours) {
1218: if (hours == 0) {
1219: return this ;
1220: }
1221: long instant = getChronology().hours().subtract(
1222: getLocalMillis(), hours);
1223: return withLocalMillis(instant);
1224: }
1225:
1226: /**
1227: * Returns a copy of this datetime minus the specified number of minutes.
1228: * <p>
1229: * This LocalDateTime instance is immutable and unaffected by this method call.
1230: * <p>
1231: * The following three lines are identical in effect:
1232: * <pre>
1233: * LocalDateTime subtracted = dt.minusMinutes(6);
1234: * LocalDateTime subtracted = dt.minus(Period.minutes(6));
1235: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.minutes(), -6);
1236: * </pre>
1237: *
1238: * @param minutes the amount of minutes to subtract, may be negative
1239: * @return the new LocalDateTime minus the increased minutes
1240: */
1241: public LocalDateTime minusMinutes(int minutes) {
1242: if (minutes == 0) {
1243: return this ;
1244: }
1245: long instant = getChronology().minutes().subtract(
1246: getLocalMillis(), minutes);
1247: return withLocalMillis(instant);
1248: }
1249:
1250: /**
1251: * Returns a copy of this datetime minus the specified number of seconds.
1252: * <p>
1253: * This LocalDateTime instance is immutable and unaffected by this method call.
1254: * <p>
1255: * The following three lines are identical in effect:
1256: * <pre>
1257: * LocalDateTime subtracted = dt.minusSeconds(6);
1258: * LocalDateTime subtracted = dt.minus(Period.seconds(6));
1259: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6);
1260: * </pre>
1261: *
1262: * @param seconds the amount of seconds to subtract, may be negative
1263: * @return the new LocalDateTime minus the increased seconds
1264: */
1265: public LocalDateTime minusSeconds(int seconds) {
1266: if (seconds == 0) {
1267: return this ;
1268: }
1269: long instant = getChronology().seconds().subtract(
1270: getLocalMillis(), seconds);
1271: return withLocalMillis(instant);
1272: }
1273:
1274: /**
1275: * Returns a copy of this datetime minus the specified number of millis.
1276: * <p>
1277: * This LocalDateTime instance is immutable and unaffected by this method call.
1278: * <p>
1279: * The following three lines are identical in effect:
1280: * <pre>
1281: * LocalDateTime subtracted = dt.minusMillis(6);
1282: * LocalDateTime subtracted = dt.minus(Period.millis(6));
1283: * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6);
1284: * </pre>
1285: *
1286: * @param millis the amount of millis to subtract, may be negative
1287: * @return the new LocalDateTime minus the increased millis
1288: */
1289: public LocalDateTime minusMillis(int millis) {
1290: if (millis == 0) {
1291: return this ;
1292: }
1293: long instant = getChronology().millis().subtract(
1294: getLocalMillis(), millis);
1295: return withLocalMillis(instant);
1296: }
1297:
1298: //-----------------------------------------------------------------------
1299: /**
1300: * Gets the property object for the specified type, which contains many
1301: * useful methods.
1302: *
1303: * @param fieldType the field type to get the chronology for
1304: * @return the property object
1305: * @throws IllegalArgumentException if the field is null or unsupported
1306: */
1307: public Property property(DateTimeFieldType fieldType) {
1308: if (fieldType == null) {
1309: throw new IllegalArgumentException(
1310: "The DateTimeFieldType must not be null");
1311: }
1312: if (isSupported(fieldType) == false) {
1313: throw new IllegalArgumentException("Field '" + fieldType
1314: + "' is not supported");
1315: }
1316: return new Property(this , fieldType.getField(getChronology()));
1317: }
1318:
1319: //-----------------------------------------------------------------------
1320: /**
1321: * Get the era field value.
1322: *
1323: * @return the era
1324: */
1325: public int getEra() {
1326: return getChronology().era().get(getLocalMillis());
1327: }
1328:
1329: /**
1330: * Get the year of era field value.
1331: *
1332: * @return the year of era
1333: */
1334: public int getCenturyOfEra() {
1335: return getChronology().centuryOfEra().get(getLocalMillis());
1336: }
1337:
1338: /**
1339: * Get the year of era field value.
1340: *
1341: * @return the year of era
1342: */
1343: public int getYearOfEra() {
1344: return getChronology().yearOfEra().get(getLocalMillis());
1345: }
1346:
1347: /**
1348: * Get the year of century field value.
1349: *
1350: * @return the year of century
1351: */
1352: public int getYearOfCentury() {
1353: return getChronology().yearOfCentury().get(getLocalMillis());
1354: }
1355:
1356: /**
1357: * Get the year field value.
1358: *
1359: * @return the year
1360: */
1361: public int getYear() {
1362: return getChronology().year().get(getLocalMillis());
1363: }
1364:
1365: /**
1366: * Get the weekyear field value.
1367: * <p>
1368: * The weekyear is the year that matches with the weekOfWeekyear field.
1369: * In the standard ISO8601 week algorithm, the first week of the year
1370: * is that in which at least 4 days are in the year. As a result of this
1371: * definition, day 1 of the first week may be in the previous year.
1372: * The weekyear allows you to query the effective year for that day.
1373: *
1374: * @return the weekyear
1375: */
1376: public int getWeekyear() {
1377: return getChronology().weekyear().get(getLocalMillis());
1378: }
1379:
1380: /**
1381: * Get the month of year field value.
1382: *
1383: * @return the month of year
1384: */
1385: public int getMonthOfYear() {
1386: return getChronology().monthOfYear().get(getLocalMillis());
1387: }
1388:
1389: /**
1390: * Get the week of weekyear field value.
1391: *
1392: * @return the week of a week based year
1393: */
1394: public int getWeekOfWeekyear() {
1395: return getChronology().weekOfWeekyear().get(getLocalMillis());
1396: }
1397:
1398: /**
1399: * Get the day of year field value.
1400: *
1401: * @return the day of year
1402: */
1403: public int getDayOfYear() {
1404: return getChronology().dayOfYear().get(getLocalMillis());
1405: }
1406:
1407: /**
1408: * Get the day of month field value.
1409: * <p>
1410: * The values for the day of month are defined in {@link org.joda.time.DateTimeConstants}.
1411: *
1412: * @return the day of month
1413: */
1414: public int getDayOfMonth() {
1415: return getChronology().dayOfMonth().get(getLocalMillis());
1416: }
1417:
1418: /**
1419: * Get the day of week field value.
1420: * <p>
1421: * The values for the day of week are defined in {@link org.joda.time.DateTimeConstants}.
1422: *
1423: * @return the day of week
1424: */
1425: public int getDayOfWeek() {
1426: return getChronology().dayOfWeek().get(getLocalMillis());
1427: }
1428:
1429: //-----------------------------------------------------------------------
1430: /**
1431: * Get the hour of day field value.
1432: *
1433: * @return the hour of day
1434: */
1435: public int getHourOfDay() {
1436: return getChronology().hourOfDay().get(getLocalMillis());
1437: }
1438:
1439: /**
1440: * Get the minute of hour field value.
1441: *
1442: * @return the minute of hour
1443: */
1444: public int getMinuteOfHour() {
1445: return getChronology().minuteOfHour().get(getLocalMillis());
1446: }
1447:
1448: /**
1449: * Get the second of minute field value.
1450: *
1451: * @return the second of minute
1452: */
1453: public int getSecondOfMinute() {
1454: return getChronology().secondOfMinute().get(getLocalMillis());
1455: }
1456:
1457: /**
1458: * Get the millis of second field value.
1459: *
1460: * @return the millis of second
1461: */
1462: public int getMillisOfSecond() {
1463: return getChronology().millisOfSecond().get(getLocalMillis());
1464: }
1465:
1466: /**
1467: * Get the millis of day field value.
1468: *
1469: * @return the millis of day
1470: */
1471: public int getMillisOfDay() {
1472: return getChronology().millisOfDay().get(getLocalMillis());
1473: }
1474:
1475: //-----------------------------------------------------------------------
1476: /**
1477: * Returns a copy of this datetime with the era field updated.
1478: * <p>
1479: * LocalDateTime is immutable, so there are no set methods.
1480: * Instead, this method returns a new instance with the value of
1481: * era changed.
1482: *
1483: * @param era the era to set
1484: * @return a copy of this object with the field set
1485: * @throws IllegalArgumentException if the value is invalid
1486: */
1487: public LocalDateTime withEra(int era) {
1488: return withLocalMillis(getChronology().era().set(
1489: getLocalMillis(), era));
1490: }
1491:
1492: /**
1493: * Returns a copy of this datetime with the century of era field updated.
1494: * <p>
1495: * LocalDateTime is immutable, so there are no set methods.
1496: * Instead, this method returns a new instance with the value of
1497: * century of era changed.
1498: *
1499: * @param centuryOfEra the centurey of era to set
1500: * @return a copy of this object with the field set
1501: * @throws IllegalArgumentException if the value is invalid
1502: */
1503: public LocalDateTime withCenturyOfEra(int centuryOfEra) {
1504: return withLocalMillis(getChronology().centuryOfEra().set(
1505: getLocalMillis(), centuryOfEra));
1506: }
1507:
1508: /**
1509: * Returns a copy of this datetime with the year of era field updated.
1510: * <p>
1511: * LocalDateTime is immutable, so there are no set methods.
1512: * Instead, this method returns a new instance with the value of
1513: * year of era changed.
1514: *
1515: * @param yearOfEra the year of era to set
1516: * @return a copy of this object with the field set
1517: * @throws IllegalArgumentException if the value is invalid
1518: */
1519: public LocalDateTime withYearOfEra(int yearOfEra) {
1520: return withLocalMillis(getChronology().yearOfEra().set(
1521: getLocalMillis(), yearOfEra));
1522: }
1523:
1524: /**
1525: * Returns a copy of this datetime with the year of century field updated.
1526: * <p>
1527: * LocalDateTime is immutable, so there are no set methods.
1528: * Instead, this method returns a new instance with the value of
1529: * year of century changed.
1530: *
1531: * @param yearOfCentury the year of century to set
1532: * @return a copy of this object with the field set
1533: * @throws IllegalArgumentException if the value is invalid
1534: */
1535: public LocalDateTime withYearOfCentury(int yearOfCentury) {
1536: return withLocalMillis(getChronology().yearOfCentury().set(
1537: getLocalMillis(), yearOfCentury));
1538: }
1539:
1540: /**
1541: * Returns a copy of this datetime with the year field updated.
1542: * <p>
1543: * LocalDateTime is immutable, so there are no set methods.
1544: * Instead, this method returns a new instance with the value of
1545: * year changed.
1546: *
1547: * @param year the year to set
1548: * @return a copy of this object with the field set
1549: * @throws IllegalArgumentException if the value is invalid
1550: */
1551: public LocalDateTime withYear(int year) {
1552: return withLocalMillis(getChronology().year().set(
1553: getLocalMillis(), year));
1554: }
1555:
1556: /**
1557: * Returns a copy of this datetime with the weekyear field updated.
1558: * <p>
1559: * LocalDateTime is immutable, so there are no set methods.
1560: * Instead, this method returns a new instance with the value of
1561: * weekyear changed.
1562: *
1563: * @param weekyear the weekyear to set
1564: * @return a copy of this object with the field set
1565: * @throws IllegalArgumentException if the value is invalid
1566: */
1567: public LocalDateTime withWeekyear(int weekyear) {
1568: return withLocalMillis(getChronology().weekyear().set(
1569: getLocalMillis(), weekyear));
1570: }
1571:
1572: /**
1573: * Returns a copy of this datetime with the month of year field updated.
1574: * <p>
1575: * LocalDateTime is immutable, so there are no set methods.
1576: * Instead, this method returns a new instance with the value of
1577: * month of year changed.
1578: *
1579: * @param monthOfYear the month of year to set
1580: * @return a copy of this object with the field set
1581: * @throws IllegalArgumentException if the value is invalid
1582: */
1583: public LocalDateTime withMonthOfYear(int monthOfYear) {
1584: return withLocalMillis(getChronology().monthOfYear().set(
1585: getLocalMillis(), monthOfYear));
1586: }
1587:
1588: /**
1589: * Returns a copy of this datetime with the week of weekyear field updated.
1590: * <p>
1591: * LocalDateTime is immutable, so there are no set methods.
1592: * Instead, this method returns a new instance with the value of
1593: * week of weekyear changed.
1594: *
1595: * @param weekOfWeekyear the week of weekyear to set
1596: * @return a copy of this object with the field set
1597: * @throws IllegalArgumentException if the value is invalid
1598: */
1599: public LocalDateTime withWeekOfWeekyear(int weekOfWeekyear) {
1600: return withLocalMillis(getChronology().weekOfWeekyear().set(
1601: getLocalMillis(), weekOfWeekyear));
1602: }
1603:
1604: /**
1605: * Returns a copy of this datetime with the day of year field updated.
1606: * <p>
1607: * LocalDateTime is immutable, so there are no set methods.
1608: * Instead, this method returns a new instance with the value of
1609: * day of year changed.
1610: *
1611: * @param dayOfYear the day of year to set
1612: * @return a copy of this object with the field set
1613: * @throws IllegalArgumentException if the value is invalid
1614: */
1615: public LocalDateTime withDayOfYear(int dayOfYear) {
1616: return withLocalMillis(getChronology().dayOfYear().set(
1617: getLocalMillis(), dayOfYear));
1618: }
1619:
1620: /**
1621: * Returns a copy of this datetime with the day of month field updated.
1622: * <p>
1623: * LocalDateTime is immutable, so there are no set methods.
1624: * Instead, this method returns a new instance with the value of
1625: * day of month changed.
1626: *
1627: * @param dayOfMonth the day of month to set
1628: * @return a copy of this object with the field set
1629: * @throws IllegalArgumentException if the value is invalid
1630: */
1631: public LocalDateTime withDayOfMonth(int dayOfMonth) {
1632: return withLocalMillis(getChronology().dayOfMonth().set(
1633: getLocalMillis(), dayOfMonth));
1634: }
1635:
1636: /**
1637: * Returns a copy of this datetime with the day of week field updated.
1638: * <p>
1639: * LocalDateTime is immutable, so there are no set methods.
1640: * Instead, this method returns a new instance with the value of
1641: * day of week changed.
1642: *
1643: * @param dayOfWeek the day of week to set
1644: * @return a copy of this object with the field set
1645: * @throws IllegalArgumentException if the value is invalid
1646: */
1647: public LocalDateTime withDayOfWeek(int dayOfWeek) {
1648: return withLocalMillis(getChronology().dayOfWeek().set(
1649: getLocalMillis(), dayOfWeek));
1650: }
1651:
1652: //-----------------------------------------------------------------------
1653: /**
1654: * Returns a copy of this datetime with the hour of day field updated.
1655: * <p>
1656: * LocalDateTime is immutable, so there are no set methods.
1657: * Instead, this method returns a new instance with the value of
1658: * hour of day changed.
1659: *
1660: * @param hour the hour of day to set
1661: * @return a copy of this object with the field set
1662: * @throws IllegalArgumentException if the value is invalid
1663: */
1664: public LocalDateTime withHourOfDay(int hour) {
1665: return withLocalMillis(getChronology().hourOfDay().set(
1666: getLocalMillis(), hour));
1667: }
1668:
1669: /**
1670: * Returns a copy of this datetime with the minute of hour field updated.
1671: * <p>
1672: * LocalDateTime is immutable, so there are no set methods.
1673: * Instead, this method returns a new instance with the value of
1674: * minute of hour changed.
1675: *
1676: * @param minute the minute of hour to set
1677: * @return a copy of this object with the field set
1678: * @throws IllegalArgumentException if the value is invalid
1679: */
1680: public LocalDateTime withMinuteOfHour(int minute) {
1681: return withLocalMillis(getChronology().minuteOfHour().set(
1682: getLocalMillis(), minute));
1683: }
1684:
1685: /**
1686: * Returns a copy of this datetime with the second of minute field updated.
1687: * <p>
1688: * LocalDateTime is immutable, so there are no set methods.
1689: * Instead, this method returns a new instance with the value of
1690: * second of minute changed.
1691: *
1692: * @param second the second of minute to set
1693: * @return a copy of this object with the field set
1694: * @throws IllegalArgumentException if the value is invalid
1695: */
1696: public LocalDateTime withSecondOfMinute(int second) {
1697: return withLocalMillis(getChronology().secondOfMinute().set(
1698: getLocalMillis(), second));
1699: }
1700:
1701: /**
1702: * Returns a copy of this datetime with the millis of second field updated.
1703: * <p>
1704: * LocalDateTime is immutable, so there are no set methods.
1705: * Instead, this method returns a new instance with the value of
1706: * millis of second changed.
1707: *
1708: * @param millis the millis of second to set
1709: * @return a copy of this object with the field set
1710: * @throws IllegalArgumentException if the value is invalid
1711: */
1712: public LocalDateTime withMillisOfSecond(int millis) {
1713: return withLocalMillis(getChronology().millisOfSecond().set(
1714: getLocalMillis(), millis));
1715: }
1716:
1717: /**
1718: * Returns a copy of this datetime with the millis of day field updated.
1719: * <p>
1720: * LocalDateTime is immutable, so there are no set methods.
1721: * Instead, this method returns a new instance with the value of
1722: * millis of day changed.
1723: *
1724: * @param millis the millis of day to set
1725: * @return a copy of this object with the field set
1726: * @throws IllegalArgumentException if the value is invalid
1727: */
1728: public LocalDateTime withMillisOfDay(int millis) {
1729: return withLocalMillis(getChronology().millisOfDay().set(
1730: getLocalMillis(), millis));
1731: }
1732:
1733: //-----------------------------------------------------------------------
1734: /**
1735: * Get the era property which provides access to advanced functionality.
1736: *
1737: * @return the era property
1738: */
1739: public Property era() {
1740: return new Property(this , getChronology().era());
1741: }
1742:
1743: /**
1744: * Get the century of era property which provides access to advanced functionality.
1745: *
1746: * @return the year of era property
1747: */
1748: public Property centuryOfEra() {
1749: return new Property(this , getChronology().centuryOfEra());
1750: }
1751:
1752: /**
1753: * Get the year of century property which provides access to advanced functionality.
1754: *
1755: * @return the year of era property
1756: */
1757: public Property yearOfCentury() {
1758: return new Property(this , getChronology().yearOfCentury());
1759: }
1760:
1761: /**
1762: * Get the year of era property which provides access to advanced functionality.
1763: *
1764: * @return the year of era property
1765: */
1766: public Property yearOfEra() {
1767: return new Property(this , getChronology().yearOfEra());
1768: }
1769:
1770: /**
1771: * Get the year property which provides access to advanced functionality.
1772: *
1773: * @return the year property
1774: */
1775: public Property year() {
1776: return new Property(this , getChronology().year());
1777: }
1778:
1779: /**
1780: * Get the weekyear property which provides access to advanced functionality.
1781: *
1782: * @return the weekyear property
1783: */
1784: public Property weekyear() {
1785: return new Property(this , getChronology().weekyear());
1786: }
1787:
1788: /**
1789: * Get the month of year property which provides access to advanced functionality.
1790: *
1791: * @return the month of year property
1792: */
1793: public Property monthOfYear() {
1794: return new Property(this , getChronology().monthOfYear());
1795: }
1796:
1797: /**
1798: * Get the week of a week based year property which provides access to advanced functionality.
1799: *
1800: * @return the week of a week based year property
1801: */
1802: public Property weekOfWeekyear() {
1803: return new Property(this , getChronology().weekOfWeekyear());
1804: }
1805:
1806: /**
1807: * Get the day of year property which provides access to advanced functionality.
1808: *
1809: * @return the day of year property
1810: */
1811: public Property dayOfYear() {
1812: return new Property(this , getChronology().dayOfYear());
1813: }
1814:
1815: /**
1816: * Get the day of month property which provides access to advanced functionality.
1817: *
1818: * @return the day of month property
1819: */
1820: public Property dayOfMonth() {
1821: return new Property(this , getChronology().dayOfMonth());
1822: }
1823:
1824: /**
1825: * Get the day of week property which provides access to advanced functionality.
1826: *
1827: * @return the day of week property
1828: */
1829: public Property dayOfWeek() {
1830: return new Property(this , getChronology().dayOfWeek());
1831: }
1832:
1833: //-----------------------------------------------------------------------
1834: /**
1835: * Get the hour of day field property which provides access to advanced functionality.
1836: *
1837: * @return the hour of day property
1838: */
1839: public Property hourOfDay() {
1840: return new Property(this , getChronology().hourOfDay());
1841: }
1842:
1843: /**
1844: * Get the minute of hour field property which provides access to advanced functionality.
1845: *
1846: * @return the minute of hour property
1847: */
1848: public Property minuteOfHour() {
1849: return new Property(this , getChronology().minuteOfHour());
1850: }
1851:
1852: /**
1853: * Get the second of minute field property which provides access to advanced functionality.
1854: *
1855: * @return the second of minute property
1856: */
1857: public Property secondOfMinute() {
1858: return new Property(this , getChronology().secondOfMinute());
1859: }
1860:
1861: /**
1862: * Get the millis of second property which provides access to advanced functionality.
1863: *
1864: * @return the millis of second property
1865: */
1866: public Property millisOfSecond() {
1867: return new Property(this , getChronology().millisOfSecond());
1868: }
1869:
1870: /**
1871: * Get the millis of day property which provides access to advanced functionality.
1872: *
1873: * @return the millis of day property
1874: */
1875: public Property millisOfDay() {
1876: return new Property(this , getChronology().millisOfDay());
1877: }
1878:
1879: //-----------------------------------------------------------------------
1880: /**
1881: * Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSS).
1882: *
1883: * @return ISO8601 time formatted string.
1884: */
1885: public String toString() {
1886: return ISODateTimeFormat.dateTime().print(this );
1887: }
1888:
1889: /**
1890: * Output the date using the specified format pattern.
1891: *
1892: * @param pattern the pattern specification, null means use <code>toString</code>
1893: * @see org.joda.time.format.DateTimeFormat
1894: */
1895: public String toString(String pattern) {
1896: if (pattern == null) {
1897: return toString();
1898: }
1899: return DateTimeFormat.forPattern(pattern).print(this );
1900: }
1901:
1902: /**
1903: * Output the date using the specified format pattern.
1904: *
1905: * @param pattern the pattern specification, null means use <code>toString</code>
1906: * @param locale Locale to use, null means default
1907: * @see org.joda.time.format.DateTimeFormat
1908: */
1909: public String toString(String pattern, Locale locale)
1910: throws IllegalArgumentException {
1911: if (pattern == null) {
1912: return toString();
1913: }
1914: return DateTimeFormat.forPattern(pattern).withLocale(locale)
1915: .print(this );
1916: }
1917:
1918: //-----------------------------------------------------------------------
1919: /**
1920: * LocalDateTime.Property binds a LocalDateTime to a DateTimeField allowing
1921: * powerful datetime functionality to be easily accessed.
1922: * <p>
1923: * The simplest use of this class is as an alternative get method, here used to
1924: * get the year '1972' (as an int) and the month 'December' (as a String).
1925: * <pre>
1926: * LocalDateTime dt = new LocalDateTime(1972, 12, 3, 0, 0);
1927: * int year = dt.year().get();
1928: * String monthStr = dt.month().getAsText();
1929: * </pre>
1930: * <p>
1931: * Methods are also provided that allow date modification. These return
1932: * new instances of LocalDateTime - they do not modify the original.
1933: * The example below yields two independent immutable date objects
1934: * 20 years apart.
1935: * <pre>
1936: * LocalDateTime dt = new LocalDateTime(1972, 12, 3, 0, 0);
1937: * LocalDateTime dt1920 = dt.year().setCopy(1920);
1938: * </pre>
1939: * <p>
1940: * LocalDateTime.Property itself is thread-safe and immutable, as well as the
1941: * LocalDateTime being operated on.
1942: *
1943: * @author Stephen Colebourne
1944: * @author Brian S O'Neill
1945: * @since 1.3
1946: */
1947: public static final class Property extends
1948: AbstractReadableInstantFieldProperty {
1949:
1950: /** Serialization version */
1951: private static final long serialVersionUID = -358138762846288L;
1952:
1953: /** The instant this property is working against */
1954: private transient LocalDateTime iInstant;
1955: /** The field this property is working against */
1956: private transient DateTimeField iField;
1957:
1958: /**
1959: * Constructor.
1960: *
1961: * @param instant the instant to set
1962: * @param field the field to use
1963: */
1964: Property(LocalDateTime instant, DateTimeField field) {
1965: super ();
1966: iInstant = instant;
1967: iField = field;
1968: }
1969:
1970: /**
1971: * Writes the property in a safe serialization format.
1972: */
1973: private void writeObject(ObjectOutputStream oos)
1974: throws IOException {
1975: oos.writeObject(iInstant);
1976: oos.writeObject(iField.getType());
1977: }
1978:
1979: /**
1980: * Reads the property from a safe serialization format.
1981: */
1982: private void readObject(ObjectInputStream oos)
1983: throws IOException, ClassNotFoundException {
1984: iInstant = (LocalDateTime) oos.readObject();
1985: DateTimeFieldType type = (DateTimeFieldType) oos
1986: .readObject();
1987: iField = type.getField(iInstant.getChronology());
1988: }
1989:
1990: //-----------------------------------------------------------------------
1991: /**
1992: * Gets the field being used.
1993: *
1994: * @return the field
1995: */
1996: public DateTimeField getField() {
1997: return iField;
1998: }
1999:
2000: /**
2001: * Gets the milliseconds of the datetime that this property is linked to.
2002: *
2003: * @return the milliseconds
2004: */
2005: protected long getMillis() {
2006: return iInstant.getLocalMillis();
2007: }
2008:
2009: /**
2010: * Gets the chronology of the datetime that this property is linked to.
2011: *
2012: * @return the chronology
2013: * @since 1.4
2014: */
2015: protected Chronology getChronology() {
2016: return iInstant.getChronology();
2017: }
2018:
2019: /**
2020: * Gets the LocalDateTime object linked to this property.
2021: *
2022: * @return the linked LocalDateTime
2023: */
2024: public LocalDateTime getLocalDateTime() {
2025: return iInstant;
2026: }
2027:
2028: //-----------------------------------------------------------------------
2029: /**
2030: * Adds to this field in a copy of this LocalDateTime.
2031: * <p>
2032: * The LocalDateTime attached to this property is unchanged by this call.
2033: *
2034: * @param value the value to add to the field in the copy
2035: * @return a copy of the LocalDateTime with the field value changed
2036: * @throws IllegalArgumentException if the value isn't valid
2037: */
2038: public LocalDateTime addToCopy(int value) {
2039: return iInstant.withLocalMillis(iField.add(iInstant
2040: .getLocalMillis(), value));
2041: }
2042:
2043: /**
2044: * Adds to this field in a copy of this LocalDateTime.
2045: * <p>
2046: * The LocalDateTime attached to this property is unchanged by this call.
2047: *
2048: * @param value the value to add to the field in the copy
2049: * @return a copy of the LocalDateTime with the field value changed
2050: * @throws IllegalArgumentException if the value isn't valid
2051: */
2052: public LocalDateTime addToCopy(long value) {
2053: return iInstant.withLocalMillis(iField.add(iInstant
2054: .getLocalMillis(), value));
2055: }
2056:
2057: /**
2058: * Adds to this field, possibly wrapped, in a copy of this LocalDateTime.
2059: * A field wrapped operation only changes this field.
2060: * Thus 31st January addWrapField one day goes to the 1st January.
2061: * <p>
2062: * The LocalDateTime attached to this property is unchanged by this call.
2063: *
2064: * @param value the value to add to the field in the copy
2065: * @return a copy of the LocalDateTime with the field value changed
2066: * @throws IllegalArgumentException if the value isn't valid
2067: */
2068: public LocalDateTime addWrapFieldToCopy(int value) {
2069: return iInstant.withLocalMillis(iField.addWrapField(
2070: iInstant.getLocalMillis(), value));
2071: }
2072:
2073: //-----------------------------------------------------------------------
2074: /**
2075: * Sets this field in a copy of the LocalDateTime.
2076: * <p>
2077: * The LocalDateTime attached to this property is unchanged by this call.
2078: *
2079: * @param value the value to set the field in the copy to
2080: * @return a copy of the LocalDateTime with the field value changed
2081: * @throws IllegalArgumentException if the value isn't valid
2082: */
2083: public LocalDateTime setCopy(int value) {
2084: return iInstant.withLocalMillis(iField.set(iInstant
2085: .getLocalMillis(), value));
2086: }
2087:
2088: /**
2089: * Sets this field in a copy of the LocalDateTime to a parsed text value.
2090: * <p>
2091: * The LocalDateTime attached to this property is unchanged by this call.
2092: *
2093: * @param text the text value to set
2094: * @param locale optional locale to use for selecting a text symbol
2095: * @return a copy of the LocalDateTime with the field value changed
2096: * @throws IllegalArgumentException if the text value isn't valid
2097: */
2098: public LocalDateTime setCopy(String text, Locale locale) {
2099: return iInstant.withLocalMillis(iField.set(iInstant
2100: .getLocalMillis(), text, locale));
2101: }
2102:
2103: /**
2104: * Sets this field in a copy of the LocalDateTime to a parsed text value.
2105: * <p>
2106: * The LocalDateTime attached to this property is unchanged by this call.
2107: *
2108: * @param text the text value to set
2109: * @return a copy of the LocalDateTime with the field value changed
2110: * @throws IllegalArgumentException if the text value isn't valid
2111: */
2112: public LocalDateTime setCopy(String text) {
2113: return setCopy(text, null);
2114: }
2115:
2116: //-----------------------------------------------------------------------
2117: /**
2118: * Returns a new LocalDateTime with this field set to the maximum value
2119: * for this field.
2120: * <p>
2121: * This operation is useful for obtaining a LocalDateTime on the last day
2122: * of the month, as month lengths vary.
2123: * <pre>
2124: * LocalDateTime lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
2125: * </pre>
2126: * <p>
2127: * The LocalDateTime attached to this property is unchanged by this call.
2128: *
2129: * @return a copy of the LocalDateTime with this field set to its maximum
2130: */
2131: public LocalDateTime withMaximumValue() {
2132: return setCopy(getMaximumValue());
2133: }
2134:
2135: /**
2136: * Returns a new LocalDateTime with this field set to the minimum value
2137: * for this field.
2138: * <p>
2139: * The LocalDateTime attached to this property is unchanged by this call.
2140: *
2141: * @return a copy of the LocalDateTime with this field set to its minimum
2142: */
2143: public LocalDateTime withMinimumValue() {
2144: return setCopy(getMinimumValue());
2145: }
2146:
2147: //-----------------------------------------------------------------------
2148: /**
2149: * Rounds to the lowest whole unit of this field on a copy of this
2150: * LocalDateTime.
2151: * <p>
2152: * For example, rounding floor on the hourOfDay field of a LocalDateTime
2153: * where the time is 10:30 would result in new LocalDateTime with the
2154: * time of 10:00.
2155: *
2156: * @return a copy of the LocalDateTime with the field value changed
2157: */
2158: public LocalDateTime roundFloorCopy() {
2159: return iInstant.withLocalMillis(iField.roundFloor(iInstant
2160: .getLocalMillis()));
2161: }
2162:
2163: /**
2164: * Rounds to the highest whole unit of this field on a copy of this
2165: * LocalDateTime.
2166: * <p>
2167: * For example, rounding floor on the hourOfDay field of a LocalDateTime
2168: * where the time is 10:30 would result in new LocalDateTime with the
2169: * time of 11:00.
2170: *
2171: * @return a copy of the LocalDateTime with the field value changed
2172: */
2173: public LocalDateTime roundCeilingCopy() {
2174: return iInstant.withLocalMillis(iField
2175: .roundCeiling(iInstant.getLocalMillis()));
2176: }
2177:
2178: /**
2179: * Rounds to the nearest whole unit of this field on a copy of this
2180: * LocalDateTime, favoring the floor if halfway.
2181: *
2182: * @return a copy of the LocalDateTime with the field value changed
2183: */
2184: public LocalDateTime roundHalfFloorCopy() {
2185: return iInstant.withLocalMillis(iField
2186: .roundHalfFloor(iInstant.getLocalMillis()));
2187: }
2188:
2189: /**
2190: * Rounds to the nearest whole unit of this field on a copy of this
2191: * LocalDateTime, favoring the ceiling if halfway.
2192: *
2193: * @return a copy of the LocalDateTime with the field value changed
2194: */
2195: public LocalDateTime roundHalfCeilingCopy() {
2196: return iInstant.withLocalMillis(iField
2197: .roundHalfCeiling(iInstant.getLocalMillis()));
2198: }
2199:
2200: /**
2201: * Rounds to the nearest whole unit of this field on a copy of this
2202: * LocalDateTime. If halfway, the ceiling is favored over the floor
2203: * only if it makes this field's value even.
2204: *
2205: * @return a copy of the LocalDateTime with the field value changed
2206: */
2207: public LocalDateTime roundHalfEvenCopy() {
2208: return iInstant.withLocalMillis(iField
2209: .roundHalfEven(iInstant.getLocalMillis()));
2210: }
2211: }
2212:
2213: }
|