下面列出了怎么用org.joda.time.ReadablePartial的API类实例代码及写法,或者点击链接到github查看源代码。
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
// overridden as superclass algorithm can't handle
// 2004-02-29 + 48 months -> 2008-02-29 type dates
if (valueToAdd == 0) {
return values;
}
// month is largest field and being added to, such as month-day
if (DateTimeUtils.isContiguous(partial)) {
long instant = 0L;
for (int i = 0, isize = partial.size(); i < isize; i++) {
instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
}
instant = add(instant, valueToAdd);
return iChronology.get(partial, instant);
} else {
return super.add(partial, fieldIndex, values, valueToAdd);
}
}
/**
* 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];
}
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
// overridden as superclass algorithm can't handle
// 2004-02-29 + 48 months -> 2008-02-29 type dates
if (valueToAdd == 0) {
return values;
}
// month is largest field and being added to, such as month-day
if (DateTimeUtils.isContiguous(partial)) {
long instant = 0L;
for (int i = 0, isize = partial.size(); i < isize; i++) {
instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
}
instant = add(instant, valueToAdd);
return iChronology.get(partial, instant);
} else {
return super.add(partial, fieldIndex, values, valueToAdd);
}
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
this.facesContext = facesContext;
Class<?> type = ValueExpressionHelper.getValueType(facesContext, uiComponent,
Lists.<Class<?>> newArrayList(DateTime.class, LocalDate.class, LocalTime.class));
Preconditions.checkArgument(type != null, "DateTimeConverter is not attached to a component bound to either a"
+ " DateTime, LocalDate, or LocalTime.");
if (value instanceof LocalDate) {
LocalDate localDate = (LocalDate) value;
return getAsStringValue(facesContext, uiComponent, localDate.toDateTimeAtStartOfDay(getTimeZone()));
}
if (value instanceof LocalTime) {
LocalTime localTime = (LocalTime) value;
return getAsStringValue(facesContext, uiComponent, localTime.toDateTimeToday(getTimeZone()));
}
if (value instanceof ReadablePartial) {
ReadablePartial readablePartial = (ReadablePartial) value;
return getAsStringValue(facesContext, uiComponent, readablePartial.toDateTime(new DateTime()));
}
this.facesContext = null;
return getAsStringValue(facesContext, uiComponent, value);
}
public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
// overridden as superclass algorithm can't handle
// 2004-02-29 + 48 months -> 2008-02-29 type dates
if (valueToAdd == 0) {
return values;
}
// month is largest field and being added to, such as month-day
if ((!(((fieldIndex) != (1)) && (valueToAdd <= values.length))) || (valueToAdd < -1)) {
long instant = 0L;
for (int i = 0, isize = partial.size(); i < isize; i++) {
instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
}
instant = add(instant, valueToAdd);
return iChronology.get(partial, instant);
} else {
return super.add(partial, fieldIndex, values, valueToAdd);
}
}
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
DateTimePrinter[] elements = iPrinters;
if (elements == null) {
throw new UnsupportedOperationException();
}
if (locale == null) {
// Guard against default locale changing concurrently.
locale = Locale.getDefault();
}
int len = elements.length;
for (int i=0; i<len; i++) {
elements[i].printTo(buf, partial, locale);
}
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
try {
out.write(print(partial, locale));
} catch (RuntimeException e) {
out.write('\ufffd');
}
}
@Override
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
if (hasMilliSecondPrecision) {
out.append(String.valueOf(getDateTimeMillis(partial)));
} else {
out.append(String.valueOf(getDateTimeMillis(partial) / 1000));
}
}
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
if (partial.isSupported(iFieldType)) {
try {
FormatUtils.appendPaddedInteger(buf, partial.get(iFieldType), iMinPrintedDigits);
} catch (RuntimeException e) {
appendUnknownString(buf, iMinPrintedDigits);
}
} else {
appendUnknownString(buf, iMinPrintedDigits);
}
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
try {
out.write(print(partial, locale));
} catch (RuntimeException e) {
out.write('\ufffd');
}
}
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
// removed check whether field is supported, as input field is typically
// secondOfDay which is unsupported by TimeOfDay
long millis = partial.getChronology().set(partial, 0L);
try {
printTo(buf, null, millis, partial.getChronology());
} catch (IOException e) {
// Not gonna happen.
}
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
if (partial.isSupported(iFieldType)) {
try {
FormatUtils.writeUnpaddedInteger(out, partial.get(iFieldType));
} catch (RuntimeException e) {
out.write('\ufffd');
}
} else {
out.write('\ufffd');
}
}
public int[] set(ReadablePartial instant, int fieldIndex, int[] values, int newValue) {
return iField.set(instant, fieldIndex, values, newValue);
}
@Override
public String print(ReadablePartial partial, Locale locale) {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(partial);
}
@Override
public int getMaximumValue(ReadablePartial instant) {
throw unsupported();
}
@Override
public String getAsShortText(ReadablePartial partial, int fieldValue, Locale locale) {
throw unsupported();
}
@Override
public String getAsText(ReadablePartial partial, Locale locale) {
throw unsupported();
}
public int[] addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd) {
return getWrappedField().addWrapField(instant, fieldIndex, values, valueToAdd);
}
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
buf.append(iValue);
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
// no zone info
}
public int getMinimumValue(ReadablePartial partial) {
return iJulianField.getMinimumValue(partial);
}
public int getMaximumValue(ReadablePartial instant, int[] values) {
return iField.getMaximumValue(instant, values);
}
public int getMinimumValue(ReadablePartial instant, int[] values) {
return iField.getMinimumValue(instant, values);
}
/**
* Sets a value using the specified partial instant.
* <p>
* The value of this field (specified by the index) will be set.
* If the value is invalid, an exception if thrown.
* <p>
* If setting this field would make other fields invalid, then those fields
* may be changed. For example if the current date is the 31st January, and
* the month is set to February, the day would be invalid. Instead, the day
* would be changed to the closest value - the 28th/29th February as appropriate.
*
* @param partial the partial instant
* @param fieldIndex the index of this field in the instant
* @param values the values to update
* @param newValue the value to set, in the units of the field
* @return the updated values
* @throws IllegalArgumentException if the value is invalid
*/
public int[] set(ReadablePartial partial, int fieldIndex, int[] values, int newValue) {
FieldUtils.verifyValueBounds(this, newValue, getMinimumValue(partial, values), getMaximumValue(partial, values));
values[fieldIndex] = newValue;
// may need to adjust smaller fields
for (int i = fieldIndex + 1; i < partial.size(); i++) {
DateTimeField field = partial.getField(i);
if (values[i] > field.getMaximumValue(partial, values)) {
values[i] = field.getMaximumValue(partial, values);
}
if (values[i] < field.getMinimumValue(partial, values)) {
values[i] = field.getMinimumValue(partial, values);
}
}
return values;
}
public int getMinimumValue(ReadablePartial partial) {
return iJulianField.getMinimumValue(partial);
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
DateTimePrinter p = getFormatter(locale).getPrinter();
p.printTo(out, partial, locale);
}
public int[] addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd) {
return getWrappedField().addWrapField(instant, fieldIndex, values, valueToAdd);
}
@Override
public String getAsText(ReadablePartial partial, int fieldValue, Locale locale) {
throw unsupported();
}
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException {
// no zone info
}
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
// no zone info
}