org.joda.time.ReadablePeriod Java Examples

The following examples show how to use org.joda.time.ReadablePeriod. 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: BaseChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param duration  the duration to query
 * @return the values of the period extracted from the duration
 */
public int[] get(ReadablePeriod period, long duration) {
    int size = period.size();
    int[] values = new int[size];
    if (duration != 0) {
        long current = 0;
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            if (field.isPrecise()) {
                int value = field.getDifference(duration, current);
                current = field.add(current, value);
                values[i] = value;
            }
        }
    }
    return values;
}
 
Example #2
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(out, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    out.write(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                out.write(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        out.write(iText);
    }
    after.printTo(out, period, locale);
}
 
Example #3
Source File: Time_27_PeriodFormatterBuilder_s.java    From coming with MIT License 6 votes vote down vote up
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    int sum = before.calculatePrintedLength(period, locale)
            + after.calculatePrintedLength(period, locale);
    
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    sum += (afterCount > 1 ? iText : iFinalText).length();
                }
            } else {
                sum += iText.length();
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        sum += iText.length();
    }
    
    return sum;
}
 
Example #4
Source File: Time_13_PeriodFormatterBuilder_t.java    From coming with MIT License 6 votes vote down vote up
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(out, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    out.write(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                out.write(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        out.write(iText);
    }
    after.printTo(out, period, locale);
}
 
Example #5
Source File: Time_27_PeriodFormatterBuilder_s.java    From coming with MIT License 6 votes vote down vote up
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(buf, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    buf.append(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                buf.append(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        buf.append(iText);
    }
    after.printTo(buf, period, locale);
}
 
Example #6
Source File: Time_27_PeriodFormatterBuilder_s.java    From coming with MIT License 6 votes vote down vote up
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(out, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    out.write(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                out.write(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        out.write(iText);
    }
    after.printTo(out, period, locale);
}
 
Example #7
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Adds the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
     for (int i = 0, isize = period.size(); i < isize; i++) {
         DurationFieldType type = period.getFieldType(i);
         int value = period.getValue(i);
         if (value != 0) {
             int index = indexOf(type);
             if (index == -1) {
                 throw new IllegalArgumentException(
                     "Period does not support field '" + type.getName() + "'");
             } else {
                 values[index] = FieldUtils.safeAdd(getValue(index), value);
             }
         }
     }
     return values;
}
 
Example #8
Source File: TestConverterManager.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testGetPeriodConverter() {
    PeriodConverter c = ConverterManager.getInstance().getPeriodConverter(new Period(1, 2, 3, 4, 5, 6, 7, 8));
    assertEquals(ReadablePeriod.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Duration(123L));
    assertEquals(ReadableDuration.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Interval(0L, 1000L));
    assertEquals(ReadableInterval.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter("");
    assertEquals(String.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(null);
    assertEquals(null, c.getSupportedType());
    
    try {
        ConverterManager.getInstance().getPeriodConverter(Boolean.TRUE);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example #9
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param duration  the duration to query
 * @return the values of the period extracted from the duration
 */
public int[] get(ReadablePeriod period, long duration) {
    int size = period.size();
    int[] values = new int[size];
    if (duration != 0) {
        long current = 0;
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            if (field.isPrecise()) {
                int value = field.getDifference(duration, current);
                current = field.add(current, value);
                values[i] = value;
            }
        }
    }
    return values;
}
 
Example #10
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 6 votes vote down vote up
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    int sum = before.calculatePrintedLength(period, locale)
            + after.calculatePrintedLength(period, locale);
    
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    sum += (afterCount > 1 ? iText : iFinalText).length();
                }
            } else {
                sum += iText.length();
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        sum += iText.length();
    }
    
    return sum;
}
 
Example #11
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 6 votes vote down vote up
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(buf, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    buf.append(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                buf.append(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        buf.append(iText);
    }
    after.printTo(buf, period, locale);
}
 
Example #12
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
    PeriodPrinter before = iBeforePrinter;
    PeriodPrinter after = iAfterPrinter;
    
    before.printTo(buf, period, locale);
    if (iUseBefore) {
        if (before.countFieldsToPrint(period, 1, locale) > 0) {
            if (iUseAfter) {
                int afterCount = after.countFieldsToPrint(period, 2, locale);
                if (afterCount > 0) {
                    buf.append(afterCount > 1 ? iText : iFinalText);
                }
            } else {
                buf.append(iText);
            }
        }
    } else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
        buf.append(iText);
    }
    after.printTo(buf, period, locale);
}
 
Example #13
Source File: Time_10_BaseSingleFieldPeriod_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Calculates the number of whole units between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, validated to not be null
 * @param end  the end partial date, validated to not be null
 * @param zeroInstance  the zero instance constant, must not be null
 * @return the period
 * @throws IllegalArgumentException if the partials are null or invalid
 */
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    if (start == null || end == null) {
        throw new IllegalArgumentException("ReadablePartial objects must not be null");
    }
    if (start.size() != end.size()) {
        throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
    }
    for (int i = 0, isize = start.size(); i < isize; i++) {
        if (start.getFieldType(i) != end.getFieldType(i)) {
            throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
        }
    }
    if (DateTimeUtils.isContiguous(start) == false) {
        throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
    }
    Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
    int[] values = chrono.get(zeroInstance, chrono.set(start, START_1972), chrono.set(end, START_1972));
    return values[0];
}
 
Example #14
Source File: Time_10_BaseSingleFieldPeriod_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a new instance representing the number of complete standard length units
 * in the specified period.
 * <p>
 * This factory method converts all fields from the period to hours using standardised
 * durations for each field. Only those fields which have a precise duration in
 * the ISO UTC chronology can be converted.
 * <ul>
 * <li>One week consists of 7 days.
 * <li>One day consists of 24 hours.
 * <li>One hour consists of 60 minutes.
 * <li>One minute consists of 60 seconds.
 * <li>One second consists of 1000 milliseconds.
 * </ul>
 * Months and Years are imprecise and periods containing these values cannot be converted.
 *
 * @param period  the period to get the number of hours from, must not be null
 * @param millisPerUnit  the number of milliseconds in one standard unit of this period
 * @throws IllegalArgumentException if the period contains imprecise duration values
 */
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    if (period == null) {
        return 0;
    }
    Chronology iso = ISOChronology.getInstanceUTC();
    long duration = 0L;
    for (int i = 0; i < period.size(); i++) {
        int value = period.getValue(i);
        if (value != 0) {
            DurationField field = period.getFieldType(i).getField(iso);
            if (field.isPrecise() == false) {
                throw new IllegalArgumentException(
                        "Cannot convert period to duration as " + field.getName() +
                        " is not precise in the period " + period);
            }
            duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
        }
    }
    return FieldUtils.safeToInt(duration / millisPerUnit);
}
 
Example #15
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
    int sum = 0;
    PeriodPrinter[] printers = iPrinters;
    for (int i=printers.length; sum < stopAt && --i>=0; ) {
        sum += printers[i].countFieldsToPrint(period, Integer.MAX_VALUE, locale);
    }
    return sum;
}
 
Example #16
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this object with the specified object for equality based on the
 * value of each field. All ReadablePeriod instances are accepted, but only
 * those with a matching <code>PeriodType</code> can return true.
 *
 * @param period  a readable period to check against
 * @return true if all the field values are equal, false if
 *  not or the period is null or of an incorrect type
 */
public boolean equals(Object period) {
    if (this == period) {
        return true;
    }
    if (period instanceof ReadablePeriod == false) {
        return false;
    }
    ReadablePeriod other = (ReadablePeriod) period;
    return (other.getPeriodType() == getPeriodType() && other.getValue(0) == getValue());
}
 
Example #17
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
    int sum = 0;
    PeriodPrinter[] printers = iPrinters;
    for (int i=printers.length; --i>=0; ) {
        sum += printers[i].calculatePrintedLength(period, locale);
    }
    return sum;
}
 
Example #18
Source File: Time_27_PeriodFormatterBuilder_t.java    From coming with MIT License 5 votes vote down vote up
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
    PeriodPrinter[] printers = iPrinters;
    int len = printers.length;
    for (int i=0; i<len; i++) {
        printers[i].printTo(out, period, locale);
    }
}
 
Example #19
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Merges the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
     for (int i = 0, isize = period.size(); i < isize; i++) {
         DurationFieldType type = period.getFieldType(i);
         int value = period.getValue(i);
         checkAndUpdate(type, values, value);
     }
     return values;
}
 
Example #20
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
    long valueLong = getFieldValue(period);
    if (valueLong == Long.MAX_VALUE) {
        return 0;
    }

    int sum = Math.max(FormatUtils.calculateDigitCount(valueLong), iMinPrintedDigits);
    if (iFieldType >= SECONDS_MILLIS) {
        // valueLong contains the seconds and millis fields
        // the minimum output is 0.000, which is 4 or 5 digits with a negative
        sum = (valueLong < 0 ? Math.max(sum, 5) : Math.max(sum, 4));
        // plus one for the decimal point
        sum++;
        if (iFieldType == SECONDS_OPTIONAL_MILLIS &&
                (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND) == 0) {
            sum -= 4; // remove three digits and decimal point
        }
        // reset valueLong to refer to the seconds part for the prefic/suffix calculation
        valueLong = valueLong / DateTimeConstants.MILLIS_PER_SECOND;
    }
    int value = (int) valueLong;

    if (iPrefix != null) {
        sum += iPrefix.calculatePrintedLength(value);
    }
    if (iSuffix != null) {
        sum += iSuffix.calculatePrintedLength(value);
    }

    return sum;
}
 
Example #21
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
    PeriodPrinter[] printers = iPrinters;
    int len = printers.length;
    for (int i=0; i<len; i++) {
        printers[i].printTo(buf, period, locale);
    }
}
 
Example #22
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
    int sum = 0;
    PeriodPrinter[] printers = iPrinters;
    for (int i=printers.length; sum < stopAt && --i>=0; ) {
        sum += printers[i].countFieldsToPrint(period, Integer.MAX_VALUE, locale);
    }
    return sum;
}
 
Example #23
Source File: PeriodFormatter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prints a ReadablePeriod to a Writer.
 *
 * @param out  the formatted period is written out
 * @param period  the period to format, not null
 */
public void printTo(Writer out, ReadablePeriod period) throws IOException {
    checkPrinter();
    checkPeriod(period);
    
    getPrinter().printTo(out, period, iLocale);
}
 
Example #24
Source File: BaseChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the values of a period from an interval.
 *
 * @param period  the period instant to use
 * @param startInstant  the start instant of an interval to query
 * @param endInstant  the start instant of an interval to query
 * @return the values of the period extracted from the interval
 */
public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
    int size = period.size();
    int[] values = new int[size];
    if (startInstant != endInstant) {
        for (int i = 0; i < size; i++) {
            DurationField field = period.getFieldType(i).getField(this);
            int value = field.getDifference(endInstant, startInstant);
            startInstant = field.add(startInstant, value);
            values[i] = value;
        }
    }
    return values;
}
 
Example #25
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example #26
Source File: Time_13_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
    long valueLong = getFieldValue(period);
    if (valueLong == Long.MAX_VALUE) {
        return;
    }
    int value = (int) valueLong;
    if (iFieldType >= SECONDS_MILLIS) {
        value = (int) (valueLong / DateTimeConstants.MILLIS_PER_SECOND);
    }

    if (iPrefix != null) {
        iPrefix.printTo(out, value);
    }
    int minDigits = iMinPrintedDigits;
    if (minDigits <= 1) {
        FormatUtils.writeUnpaddedInteger(out, value);
    } else {
        FormatUtils.writePaddedInteger(out, value, minDigits);
    }
    if (iFieldType >= SECONDS_MILLIS) {
        int dp = (int) (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND);
        if (iFieldType == SECONDS_MILLIS || dp > 0) {
            out.write('.');
            FormatUtils.writePaddedInteger(out, dp, 3);
        }
    }
    if (iSuffix != null) {
        iSuffix.printTo(out, value);
    }
}
 
Example #27
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
    if (stopAt <= 0) {
        return 0;
    }
    if (iPrintZeroSetting == PRINT_ZERO_ALWAYS || getFieldValue(period) != Long.MAX_VALUE) {
        return 1;
    }
    return 0;
}
 
Example #28
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Merges the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, values, value);
    }
    return values;
}
 
Example #29
Source File: PeriodFormatterBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
Example #30
Source File: Time_27_PeriodFormatterBuilder_s.java    From coming with MIT License 5 votes vote down vote up
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}