/** * Prints a ReadableInstant, using the chronology supplied by the instant. * * @param appendable the destination to format to, not null * @param instant instant to format, null means now * @since 2.0 */ public void printTo(Appendable appendable, ReadableInstant instant) throws IOException { long millis = DateTimeUtils.getInstantMillis(instant); Chronology chrono = DateTimeUtils.getInstantChronology(instant); printTo(appendable, millis, chrono); }
/** * Prints a ReadableInstant, using the chronology supplied by the instant. * * @param appendable the destination to format to, not null * @param instant instant to format, null means now * @since 2.0 */ public void printTo(Appendable appendable, ReadableInstant instant) throws IOException { long millis = DateTimeUtils.getInstantMillis(instant); Chronology chrono = DateTimeUtils.getInstantChronology(instant); printTo(appendable, millis, chrono); }
/** * Resolves this partial against another complete instant to create a new * full instant. The combination is performed using the chronology of the * specified instant. * <p> * For example, if this partial represents a time, then the result of this * method will be the datetime from the specified base instant plus the * time from this partial. * * @param baseInstant the instant that provides the missing fields, null means now * @return the combined datetime */ public DateTime toDateTime(ReadableInstant baseInstant) { Chronology chrono = DateTimeUtils.getInstantChronology(baseInstant); long instantMillis = DateTimeUtils.getInstantMillis(baseInstant); long resolved = chrono.set(this, instantMillis); return new DateTime(resolved, chrono); }
/** * Calculates the number of whole units between the two specified datetimes. * * @param start the start instant, validated to not be null * @param end the end instant, validated to not be null * @param field the field type to use, must not be null * @return the period * @throws IllegalArgumentException if the instants are null or invalid */ protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) { if (start == null || end == null) { throw new IllegalArgumentException("ReadableInstant objects must not be null"); } Chronology chrono = DateTimeUtils.getInstantChronology(start); int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis()); return amount; }
/** * Calculates the number of whole units between the two specified datetimes. * * @param start the start instant, validated to not be null * @param end the end instant, validated to not be null * @param field the field type to use, must not be null * @return the period * @throws IllegalArgumentException if the instants are null or invalid */ protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) { if (start == null || end == null) { throw new IllegalArgumentException("ReadableInstant objects must not be null"); } Chronology chrono = DateTimeUtils.getInstantChronology(start); int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis()); return amount; }
/** * Does this partial match the specified instant. * <p> * A match occurs when all the fields of this partial are the same as the * corresponding fields on the specified instant. * * @param instant an instant to check against, null means now in default zone * @return true if this partial matches the specified instant */ public boolean isMatch(ReadableInstant instant) { long millis = DateTimeUtils.getInstantMillis(instant); Chronology chrono = DateTimeUtils.getInstantChronology(instant); for (int i = 0; i < iTypes.length; i++) { int value = iTypes[i].getField(chrono).get(millis); if (value != iValues[i]) { return false; } } return true; }
/** * Constructs an interval from a millisecond duration and an end instant. * * @param duration the duration of this interval, null means zero length * @param end end of this interval, null means now * @throws IllegalArgumentException if the end is before the start * @throws ArithmeticException if the start instant exceeds the capacity of a long */ protected BaseInterval(ReadableDuration duration, ReadableInstant end) { super(); iChronology = DateTimeUtils.getInstantChronology(end); iEndMillis = DateTimeUtils.getInstantMillis(end); long durationMillis = DateTimeUtils.getDurationMillis(duration); iStartMillis = FieldUtils.safeAdd(iEndMillis, -durationMillis); checkInterval(iStartMillis, iEndMillis); }
/** * Constructs an interval from a start instant and a duration. * * @param start start of this interval, null means now * @param duration the duration of this interval, null means zero length * @throws IllegalArgumentException if the end is before the start * @throws ArithmeticException if the end instant exceeds the capacity of a long */ protected BaseInterval(ReadableInstant start, ReadableDuration duration) { super(); iChronology = DateTimeUtils.getInstantChronology(start); iStartMillis = DateTimeUtils.getInstantMillis(start); long durationMillis = DateTimeUtils.getDurationMillis(duration); iEndMillis = FieldUtils.safeAdd(iStartMillis, durationMillis); checkInterval(iStartMillis, iEndMillis); }
/** * Set the time from another instant. * The date part of this object will be unaffected. * * @param instant an instant to copy the time from, date part ignored * @throws IllegalArgumentException if the object is invalid */ public void setTime(final ReadableInstant instant) { long instantMillis = DateTimeUtils.getInstantMillis(instant); Chronology instantChrono = DateTimeUtils.getInstantChronology(instant); DateTimeZone zone = instantChrono.getZone(); if (zone != null) { instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis); } setTime(instantMillis); }
/** * Constructs an interval from a start instant and a duration. * * @param start start of this interval, null means now * @param duration the duration of this interval, null means zero length * @throws IllegalArgumentException if the end is before the start * @throws ArithmeticException if the end instant exceeds the capacity of a long */ protected BaseInterval(ReadableInstant start, ReadableDuration duration) { super(); iChronology = DateTimeUtils.getInstantChronology(start); iStartMillis = DateTimeUtils.getInstantMillis(start); long durationMillis = DateTimeUtils.getDurationMillis(duration); iEndMillis = FieldUtils.safeAdd(iStartMillis, durationMillis); checkInterval(iStartMillis, iEndMillis); }
/** * Constructs an interval from a millisecond duration and an end instant. * * @param duration the duration of this interval, null means zero length * @param end end of this interval, null means now * @throws IllegalArgumentException if the end is before the start * @throws ArithmeticException if the start instant exceeds the capacity of a long */ protected BaseInterval(ReadableDuration duration, ReadableInstant end) { super(); iChronology = DateTimeUtils.getInstantChronology(end); iEndMillis = DateTimeUtils.getInstantMillis(end); long durationMillis = DateTimeUtils.getDurationMillis(duration); iStartMillis = FieldUtils.safeAdd(iEndMillis, -durationMillis); checkInterval(iStartMillis, iEndMillis); }
/** * Set the time from another instant. * The date part of this object will be unaffected. * * @param instant an instant to copy the time from, date part ignored * @throws IllegalArgumentException if the object is invalid */ public void setTime(final ReadableInstant instant) { long instantMillis = DateTimeUtils.getInstantMillis(instant); Chronology instantChrono = DateTimeUtils.getInstantChronology(instant); DateTimeZone zone = instantChrono.getZone(); if (zone != null) { instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis); } setTime(instantMillis); }
/** * Creates a period from the given duration and end point. * * @param duration the duration of the interval, null means zero-length * @param endInstant the interval end, null means now * @param type which set of fields this period supports, null means standard */ protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) { super(); type = checkPeriodType(type); long durationMillis = DateTimeUtils.getDurationMillis(duration); long endMillis = DateTimeUtils.getInstantMillis(endInstant); long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis); Chronology chrono = DateTimeUtils.getInstantChronology(endInstant); iType = type; iValues = chrono.get(this, startMillis, endMillis); }
/** * Creates a period from the given start point and duration. * * @param startInstant the interval start, null means now * @param duration the duration of the interval, null means zero-length * @param type which set of fields this period supports, null means standard */ protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) { super(); type = checkPeriodType(type); long startMillis = DateTimeUtils.getInstantMillis(startInstant); long durationMillis = DateTimeUtils.getDurationMillis(duration); long endMillis = FieldUtils.safeAdd(startMillis, durationMillis); Chronology chrono = DateTimeUtils.getInstantChronology(startInstant); iType = type; iValues = chrono.get(this, startMillis, endMillis); }
private static long millisBetween(ReadableInstant start, ReadableInstant end) { requireNonNull(start, "start is null"); requireNonNull(end, "end is null"); return millis().getField(getInstantChronology(start)).getDifferenceAsLong(end.getMillis(), start.getMillis()); }
/** * Creates a period from the given start point and duration. * * @param startInstant the interval start, null means now * @param duration the duration of the interval, null means zero-length * @param type which set of fields this period supports, null means standard */ protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) { super(); type = checkPeriodType(type); long startMillis = DateTimeUtils.getInstantMillis(startInstant); long durationMillis = DateTimeUtils.getDurationMillis(duration); long endMillis = FieldUtils.safeAdd(startMillis, durationMillis); Chronology chrono = DateTimeUtils.getInstantChronology(startInstant); iType = type; iValues = chrono.get(this, startMillis, endMillis); }
/** * Constructs an interval from a start and end instant. * * @param start start of this interval, null means now * @param end end of this interval, null means now * @throws IllegalArgumentException if the end is before the start */ protected BaseInterval(ReadableInstant start, ReadableInstant end) { super(); if (start == null && end == null) { iStartMillis = iEndMillis = DateTimeUtils.currentTimeMillis(); iChronology = ISOChronology.getInstance(); } else { iChronology = DateTimeUtils.getInstantChronology(start); iStartMillis = DateTimeUtils.getInstantMillis(start); iEndMillis = DateTimeUtils.getInstantMillis(end); checkInterval(iStartMillis, iEndMillis); } }
/** * Creates a period from the given duration and end point. * * @param duration the duration of the interval, null means zero-length * @param endInstant the interval end, null means now * @param type which set of fields this period supports, null means standard */ protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) { super(); type = checkPeriodType(type); long durationMillis = DateTimeUtils.getDurationMillis(duration); long endMillis = DateTimeUtils.getInstantMillis(endInstant); long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis); Chronology chrono = DateTimeUtils.getInstantChronology(endInstant); iType = type; iValues = chrono.get(this, startMillis, endMillis); }
/** * Sets this interval from two instants, replacing the chronology with * that from the start instant. * * @param start the start of the time interval * @param end the start of the time interval * @throws IllegalArgumentException if the end is before the start */ public void setInterval(ReadableInstant start, ReadableInstant end) { if (start == null && end == null) { long now = DateTimeUtils.currentTimeMillis(); setInterval(now, now); } else { long startMillis = DateTimeUtils.getInstantMillis(start); long endMillis = DateTimeUtils.getInstantMillis(end); Chronology chrono = DateTimeUtils.getInstantChronology(start); super.setInterval(startMillis, endMillis, chrono); } }
/** * Sets this interval from two instants, replacing the chronology with * that from the start instant. * * @param start the start of the time interval * @param end the start of the time interval * @throws IllegalArgumentException if the end is before the start */ public void setInterval(ReadableInstant start, ReadableInstant end) { if (start == null && end == null) { long now = DateTimeUtils.currentTimeMillis(); setInterval(now, now); } else { long startMillis = DateTimeUtils.getInstantMillis(start); long endMillis = DateTimeUtils.getInstantMillis(end); Chronology chrono = DateTimeUtils.getInstantChronology(start); super.setInterval(startMillis, endMillis, chrono); } }