Java Code Examples for org.joda.time.Chronology#add()

The following examples show how to use org.joda.time.Chronology#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a start instant and a time period.
 * <p>
 * When forming the interval, the chronology from the instant is used
 * if present, otherwise the chronology of the period is used.
 * 
 * @param start  start of this interval, null means now
 * @param period  the period 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, ReadablePeriod period) {
    super();
    Chronology chrono = DateTimeUtils.getInstantChronology(start);
    iChronology = chrono;
    iStartMillis = DateTimeUtils.getInstantMillis(start);
    if (period == null) {
        iEndMillis = iStartMillis;
    } else {
        iEndMillis = chrono.add(period, iStartMillis, 1);
    }
    checkInterval(iStartMillis, iEndMillis);
}
 
Example 2
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a time period and an end instant.
 * <p>
 * When forming the interval, the chronology from the instant is used
 * if present, otherwise the chronology of the period is used.
 * 
 * @param period  the period 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(ReadablePeriod period, ReadableInstant end) {
    super();
    Chronology chrono = DateTimeUtils.getInstantChronology(end);
    iChronology = chrono;
    iEndMillis = DateTimeUtils.getInstantMillis(end);
    if (period == null) {
        iStartMillis = iEndMillis;
    } else {
        iStartMillis = chrono.add(period, iEndMillis, -1);
    }
    checkInterval(iStartMillis, iEndMillis);
}
 
Example 3
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a start instant and a time period.
 * <p>
 * When forming the interval, the chronology from the instant is used
 * if present, otherwise the chronology of the period is used.
 * 
 * @param start  start of this interval, null means now
 * @param period  the period 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, ReadablePeriod period) {
    super();
    Chronology chrono = DateTimeUtils.getInstantChronology(start);
    iChronology = chrono;
    iStartMillis = DateTimeUtils.getInstantMillis(start);
    if (period == null) {
        iEndMillis = iStartMillis;
    } else {
        iEndMillis = chrono.add(period, iStartMillis, 1);
    }
    checkInterval(iStartMillis, iEndMillis);
}
 
Example 4
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a time period and an end instant.
 * <p>
 * When forming the interval, the chronology from the instant is used
 * if present, otherwise the chronology of the period is used.
 * 
 * @param period  the period 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(ReadablePeriod period, ReadableInstant end) {
    super();
    Chronology chrono = DateTimeUtils.getInstantChronology(end);
    iChronology = chrono;
    iEndMillis = DateTimeUtils.getInstantMillis(end);
    if (period == null) {
        iStartMillis = iEndMillis;
    } else {
        iStartMillis = chrono.add(period, iEndMillis, -1);
    }
    checkInterval(iStartMillis, iEndMillis);
}
 
Example 5
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 6
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 7
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to a start instant.
 * <p>
 * This method adds the period to the specified instant in order to
 * calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param startInstant  the instant to add the period to, thus obtaining the duration
 * @return the total length of the period as a duration relative to the start instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationFrom(ReadableInstant startInstant) {
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    long endMillis = chrono.add(this, startMillis, 1);
    return new Duration(startMillis, endMillis);
}
 
Example 8
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to an
 * end instant.
 * <p>
 * This method subtracts the period from the specified instant in order
 * to calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param endInstant  the instant to subtract the period from, thus obtaining the duration
 * @return the total length of the period as a duration relative to the end instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationTo(ReadableInstant endInstant) {
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    long startMillis = chrono.add(this, endMillis, -1);
    return new Duration(startMillis, endMillis);
}
 
Example 9
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to a start instant.
 * <p>
 * This method adds the period to the specified instant in order to
 * calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param startInstant  the instant to add the period to, thus obtaining the duration
 * @return the total length of the period as a duration relative to the start instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationFrom(ReadableInstant startInstant) {
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    long endMillis = chrono.add(this, startMillis, 1);
    return new Duration(startMillis, endMillis);
}
 
Example 10
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to an
 * end instant.
 * <p>
 * This method subtracts the period from the specified instant in order
 * to calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param endInstant  the instant to subtract the period from, thus obtaining the duration
 * @return the total length of the period as a duration relative to the end instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationTo(ReadableInstant endInstant) {
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    long startMillis = chrono.add(this, endMillis, -1);
    return new Duration(startMillis, endMillis);
}
 
Example 11
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to a start instant.
 * <p>
 * This method adds the period to the specified instant in order to
 * calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param startInstant  the instant to add the period to, thus obtaining the duration
 * @return the total length of the period as a duration relative to the start instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationFrom(ReadableInstant startInstant) {
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    long endMillis = chrono.add(this, startMillis, 1);
    return new Duration(startMillis, endMillis);
}
 
Example 12
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to an
 * end instant.
 * <p>
 * This method subtracts the period from the specified instant in order
 * to calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param endInstant  the instant to subtract the period from, thus obtaining the duration
 * @return the total length of the period as a duration relative to the end instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationTo(ReadableInstant endInstant) {
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    long startMillis = chrono.add(this, endMillis, -1);
    return new Duration(startMillis, endMillis);
}
 
Example 13
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to a start instant.
 * <p>
 * This method adds the period to the specified instant in order to
 * calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param startInstant  the instant to add the period to, thus obtaining the duration
 * @return the total length of the period as a duration relative to the start instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationFrom(ReadableInstant startInstant) {
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    long endMillis = chrono.add(this, startMillis, 1);
    return new Duration(startMillis, endMillis);
}
 
Example 14
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the total millisecond duration of this period relative to an
 * end instant.
 * <p>
 * This method subtracts the period from the specified instant in order
 * to calculate the duration.
 * <p>
 * An instant must be supplied as the duration of a period varies.
 * For example, a period of 1 month could vary between the equivalent of
 * 28 and 31 days in milliseconds due to different length months.
 * Similarly, a day can vary at Daylight Savings cutover, typically between
 * 23 and 25 hours.
 *
 * @param endInstant  the instant to subtract the period from, thus obtaining the duration
 * @return the total length of the period as a duration relative to the end instant
 * @throws ArithmeticException if the millis exceeds the capacity of the duration
 */
public Duration toDurationTo(ReadableInstant endInstant) {
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    long startMillis = chrono.add(this, endMillis, -1);
    return new Duration(startMillis, endMillis);
}