类org.joda.time.ReadablePeriod源码实例Demo

下面列出了怎么用org.joda.time.ReadablePeriod的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: astor   文件: PeriodFormatterBuilder.java
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);
}
 
源代码2 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
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;
}
 
源代码3 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
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);
}
 
源代码4 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
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);
}
 
源代码5 项目: coming   文件: Time_22_BasePeriod_t.java
/**
 * 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;
}
 
源代码6 项目: astor   文件: TestConverterManager.java
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) {}
}
 
源代码7 项目: astor   文件: BaseChronology.java
/**
 * 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;
}
 
源代码8 项目: coming   文件: Time_13_PeriodFormatterBuilder_s.java
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;
}
 
源代码9 项目: coming   文件: Time_13_PeriodFormatterBuilder_s.java
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);
}
 
源代码10 项目: coming   文件: Time_10_BaseSingleFieldPeriod_t.java
/**
 * 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];
}
 
源代码11 项目: coming   文件: Time_10_BaseSingleFieldPeriod_t.java
/**
 * 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);
}
 
源代码12 项目: astor   文件: BaseChronology.java
/**
 * 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;
}
 
源代码13 项目: astor   文件: PeriodFormatterBuilder.java
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);
}
 
源代码14 项目: coming   文件: Time_13_PeriodFormatterBuilder_t.java
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);
}
 
源代码15 项目: astor   文件: PeriodFormatterBuilder.java
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;
}
 
源代码16 项目: astor   文件: PeriodFormatter.java
/**
 * Prints a ReadablePeriod to a StringBuffer.
 *
 * @param buf  the formatted period is appended to this buffer
 * @param period  the period to format, not null
 */
public void printTo(StringBuffer buf, ReadablePeriod period) {
    checkPrinter();
    checkPeriod(period);
    
    getPrinter().printTo(buf, period, iLocale);
}
 
源代码17 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
boolean isZero(ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        if (period.getValue(i) != 0) {
            return false;
        }
    }
    return true;
}
 
源代码18 项目: astor   文件: BasePeriod.java
/**
 * Sets all the fields of this period from another.
 * 
 * @param period  the period to copy from, not null
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected void setPeriod(ReadablePeriod period) {
    if (period == null) {
        setValues(new int[size()]);
    } else {
        setPeriodInternal(period);
    }
}
 
源代码19 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
    int sum = iBeforePrinter.countFieldsToPrint(period, stopAt, locale);
    if (sum < stopAt) {
        sum += iAfterPrinter.countFieldsToPrint(period, stopAt, locale);
    }
    return sum;
}
 
源代码20 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
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;
}
 
源代码21 项目: coming   文件: Time_27_PeriodFormatterBuilder_s.java
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;
}
 
源代码22 项目: astor   文件: BaseInterval.java
/**
 * 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);
}
 
源代码23 项目: coming   文件: Time_27_PeriodFormatterBuilder_t.java
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
    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(buf, value);
    }
    int minDigits = iMinPrintedDigits;
    if (minDigits <= 1) {
        FormatUtils.appendUnpaddedInteger(buf, value);
    } else {
        FormatUtils.appendPaddedInteger(buf, value, minDigits);
    }
    if (iFieldType >= SECONDS_MILLIS) {
        int dp = (int) (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND);
        if (iFieldType == SECONDS_MILLIS || dp > 0) {
            buf.append('.');
            FormatUtils.appendPaddedInteger(buf, dp, 3);
        }
    }
    if (iSuffix != null) {
        iSuffix.printTo(buf, value);
    }
}
 
源代码24 项目: coming   文件: Time_27_PeriodFormatterBuilder_t.java
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);
    }
}
 
源代码25 项目: astor   文件: BaseChronology.java
/**
 * Adds the period to the instant, specifying the number of times to add.
 *
 * @param period  the period to add, null means add nothing
 * @param instant  the instant to add to
 * @param scalar  the number of times to add
 * @return the updated instant
 */
public long add(ReadablePeriod period, long instant, int scalar) {
    if (scalar != 0 && period != null) {
        for (int i = 0, isize = period.size(); i < isize; i++) {
            long value = period.getValue(i); // use long to allow for multiplication (fits OK)
            if (value != 0) {
                instant = period.getFieldType(i).getField(this).add(instant, value * scalar);
            }
        }
    }
    return instant;
}
 
源代码26 项目: jfixture   文件: ReadablePeriodRelay.java
@Override
public Object create(Object request, SpecimenContext context) {
    if (!(request.equals(ReadablePeriod.class) || request.equals(ReadWritablePeriod.class)))
        return new NoSpecimen();

    DateTime dateA = (DateTime) context.resolve(DateTime.class);
    DateTime dateB = (DateTime) context.resolve(DateTime.class);

    if (dateA.isBefore(dateB))
        return new MutablePeriod(dateA, dateB);
    else
        return new MutablePeriod(dateB, dateA);
}
 
源代码27 项目: astor   文件: PeriodFormatter.java
/**
 * 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);
}
 
源代码28 项目: coming   文件: Time_27_PeriodFormatterBuilder_t.java
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;
}
 
源代码29 项目: coming   文件: Time_27_PeriodFormatterBuilder_t.java
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);
    }
}
 
源代码30 项目: coming   文件: Time_27_PeriodFormatterBuilder_t.java
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);
    }
}
 
 类所在包
 同包方法