org.joda.time.Partial类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(151)

本文整理了Java中org.joda.time.Partial类的一些代码示例,展示了Partial类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Partial类的具体详情如下:
包路径:org.joda.time.Partial
类名称:Partial

Partial介绍

[英]Partial is an immutable partial datetime supporting any set of datetime fields.

A Partial instance can be used to hold any combination of fields. The instance does not contain a time zone, so any datetime is local.

A Partial can be matched against an instant using #isMatch(ReadableInstant). This method compares each field on this partial with those of the instant and determines if the partial matches the instant. Given this definition, an empty Partial instance represents any datetime and always matches.

Calculations on Partial are performed using a Chronology. This chronology is set to be in the UTC time zone for all calculations.

Each individual field can be queried in two ways:

  • get(DateTimeFieldType.monthOfYear())

  • property(DateTimeFieldType.monthOfYear()).get()
    The second technique also provides access to other useful methods on the field:

  • numeric value - monthOfYear().get()

  • text value - monthOfYear().getAsText()

  • short text value - monthOfYear().getAsShortText()

  • maximum/minimum values - monthOfYear().getMaximumValue()

  • add/subtract - monthOfYear().addToCopy()

  • set - monthOfYear().setCopy()

Partial is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable.
[中]Partial是一个不可变的Partial datetime,支持任何一组datetime字段。
部分实例可用于保存字段的任意组合。该实例不包含时区,因此任何datetime都是本地的。
可以使用#isMatch(ReadableInstant)将部分内容与瞬间内容进行匹配。此方法将此部分上的每个字段与即时字段进行比较,并确定该部分是否与即时字段匹配。根据这个定义,空的部分实例表示任何日期时间,并且始终匹配。
使用年表对部分数据进行计算。对于所有计算,该年表设置为UTC时区。
可以通过两种方式查询每个字段:

  • get(DateTimeFieldType.monthOfYear())
  • property(DateTimeFieldType.monthOfYear()).get()
    第二种技术还提供了现场其他有用方法:
    *数值-monthOfYear().get()
    *文本值-monthOfYear().getAsText()
    *短文本值-monthOfYear().getAsShortText()
    *最大值/最小值-monthOfYear().getMaximumValue()
    *加/减-monthOfYear().addToCopy()
    *套装-monthOfYear().setCopy()
    Partial是线程安全且不可变的,前提是年表也是。提供的所有标准时序类都是线程安全且不可变的。

代码示例

代码示例来源:origin: stanfordnlp/CoreNLP

public static Partial discardMoreSpecificFields(Partial p, DateTimeFieldType d)
{
 Partial res = new Partial();
 for (int i = 0; i < p.size(); i++) {
  DateTimeFieldType fieldType = p.getFieldType(i);
  if (fieldType.equals(d) || isMoreGeneral(fieldType, d, p.getChronology())) {
   res = res.with(fieldType, p.getValue(i));
  }
 }
 if (res.isSupported(JodaTimeUtils.DecadeOfCentury) && !res.isSupported(DateTimeFieldType.centuryOfEra())) {
  if (p.isSupported(DateTimeFieldType.year())) {
   res = res.with(DateTimeFieldType.centuryOfEra(), p.get(DateTimeFieldType.year()) / 100);
  }
 }
 return res;
}

代码示例来源:origin: joda-time/joda-time

/**
 * Gets a copy of this instance with the specified period added.
 * <p>
 * If the amount is zero or null, then <code>this</code> is returned.
 *
 * @param period  the duration to add to this one, null means zero
 * @return a copy of this instance with the period added
 * @throws ArithmeticException if the new datetime exceeds the capacity of a long
 */
public Partial plus(ReadablePeriod period) {
  return withPeriodAdded(period, 1);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date using the specified format pattern.
 * Unsupported fields will appear as special unicode characters.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern) {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).print(this);
}

代码示例来源:origin: stanfordnlp/CoreNLP

protected static DateTimeFieldType getMostGeneral(Partial p)
{
 if (p.size() > 0) { return p.getFieldType(0); }
 return null;
}
protected static DateTimeFieldType getMostSpecific(Partial p)

代码示例来源:origin: stanfordnlp/CoreNLP

protected static Partial setField(Partial base, DateTimeFieldType field, int value) {
 if (base == null) {
  return new Partial(field, value);
 } else {
  return base.with(field, value);
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static Partial withWeekYear(Partial p)
{
 Partial res = new Partial();
 for (int i = 0; i < p.size(); i++) {
  DateTimeFieldType fieldType = p.getFieldType(i);
  if (fieldType == DateTimeFieldType.year()) {
   res = res.with(DateTimeFieldType.weekyear(), p.getValue(i));
  } else {
   res = res.with(fieldType, p.getValue(i));
  }
 }
 return res;
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date in an appropriate ISO8601 format.
 * <p>
 * This method will output the partial in one of two ways.
 * If {@link #getFormatter()}
 * <p>
 * If there is no appropriate ISO format a dump of the fields is output
 * via {@link #toStringList()}.
 * 
 * @return ISO8601 formatted string
 */
public String toString() {
  DateTimeFormatter[] f = iFormatter;
  if (f == null) {
    getFormatter();
    f = iFormatter;
    if (f == null) {
      return toStringList();
    }
  }
  DateTimeFormatter f1 = f[1];
  if (f1 == null) {
    return toStringList();
  }
  return f1.print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date using the specified format pattern.
 * Unsupported fields will appear as special unicode characters.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: stanfordnlp/CoreNLP

if (isMoreGeneral(msf, DateTimeFieldType.year(), p.getChronology()) ||
    isMoreGeneral(msf, DateTimeFieldType.yearOfCentury(), p.getChronology())) {
 if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
  if (p.isSupported(JodaTimeUtils.DecadeOfCentury)) {
   if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
    int year = p.get(DateTimeFieldType.centuryOfEra()) * 100 + p.get(JodaTimeUtils.DecadeOfCentury)*10;
    p = p.without(JodaTimeUtils.DecadeOfCentury);
    p = p.without(DateTimeFieldType.centuryOfEra());
    p = p.with(DateTimeFieldType.year(), year);
   } else {
    int year = p.get(JodaTimeUtils.DecadeOfCentury)*10;
    p = p.without(JodaTimeUtils.DecadeOfCentury);
    p = p.with(DateTimeFieldType.yearOfCentury(), year);
   if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
    int year = p.get(DateTimeFieldType.centuryOfEra()) * 100;
    p = p.without(DateTimeFieldType.centuryOfEra());
    p = p.with(DateTimeFieldType.year(), year);
if (p.isSupported(DateTimeFieldType.weekOfWeekyear())) {
 if (!p.isSupported(DateTimeFieldType.dayOfMonth()) && !p.isSupported(DateTimeFieldType.dayOfWeek())) {
  p = p.with(DateTimeFieldType.dayOfWeek(), 1);
  if (p.isSupported(DateTimeFieldType.monthOfYear())) {
   p = p.without(DateTimeFieldType.monthOfYear());
for (int i = 0; i < p2.size(); i++) {
 DateTimeFieldType fieldType = p2.getFieldType(i);
 if (msf == null || isMoreSpecific(fieldType, msf, p.getChronology())) {

代码示例来源:origin: stanfordnlp/CoreNLP

if (p2 == null) return p1;
Partial p = p1;
for (int i = 0; i < p2.size(); i++) {
 DateTimeFieldType fieldType = p2.getFieldType(i);
 if (fieldType == DateTimeFieldType.year()) {
  if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
   if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
    int yoc = p.get(DateTimeFieldType.yearOfCentury());
    int refYear = p2.getValue(i);
    int century = refYear / 100;
    int y2 = yoc + century*100;
    p = p.without(DateTimeFieldType.yearOfCentury());
    p = p.with(DateTimeFieldType.year(), y2);
  } else if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
   continue;
  if (p.isSupported(DateTimeFieldType.year())) {
   continue;
  if (p.isSupported(DateTimeFieldType.year())) {
   continue;
 if (!p.isSupported(fieldType)) {
  p = p.with(fieldType, p2.getValue(i));
if (!p.isSupported(DateTimeFieldType.year())) {
 if (p.isSupported(DateTimeFieldType.yearOfCentury()) && p.isSupported(DateTimeFieldType.centuryOfEra())) {

代码示例来源:origin: joda-time/joda-time

throw new IllegalArgumentException("The field type must not be null");
int index = indexOf(fieldType);
if (index == -1) {
  DateTimeFieldType[] newTypes = new DateTimeFieldType[iTypes.length + 1];
          break;
        } else if (compare == 0) {
          if (fieldType.getRangeDurationType() == null) {
            break;
  Partial newPartial = new Partial(newTypes, newValues, iChronology);
  iChronology.validate(newPartial, newValues);
  return newPartial;
if (value == getValue(index)) {
  return this;
int[] newValues = getValues();
newValues = getField(index).set(this, index, newValues, value);
return new Partial(this, newValues);

代码示例来源:origin: stanfordnlp/CoreNLP

public List<Temporal> toList() {
 if (JodaTimeUtils.hasField(base, DateTimeFieldType.year())
   && JodaTimeUtils.hasField(base, DateTimeFieldType.monthOfYear())
   && JodaTimeUtils.hasField(base, DateTimeFieldType.dayOfWeek())) {
  List<Temporal> list = new ArrayList<>();
  Partial pt = new Partial();
  pt = JodaTimeUtils.setField(pt, DateTimeFieldType.year(), base.get(DateTimeFieldType.year()));
  pt = JodaTimeUtils.setField(pt, DateTimeFieldType.monthOfYear(), base.get(DateTimeFieldType.monthOfYear()));
  pt = JodaTimeUtils.setField(pt, DateTimeFieldType.dayOfMonth(), 1);
  Partial candidate = JodaTimeUtils.resolveDowToDay(base, pt);
  if (candidate.get(DateTimeFieldType.monthOfYear()) != base.get(DateTimeFieldType.monthOfYear())) {
   pt = JodaTimeUtils.setField(pt, DateTimeFieldType.dayOfMonth(), 8);
   candidate = JodaTimeUtils.resolveDowToDay(base, pt);
   if (candidate.get(DateTimeFieldType.monthOfYear()) != base.get(DateTimeFieldType.monthOfYear())) {
    // give up
    return null;
   }
  }
  try {
   while (candidate.get(DateTimeFieldType.monthOfYear()) == base.get(DateTimeFieldType.monthOfYear())) {
    list.add(new PartialTime(this, candidate));
    pt = JodaTimeUtils.setField(pt, DateTimeFieldType.dayOfMonth(), pt.get(DateTimeFieldType.dayOfMonth()) + 7);
    candidate = JodaTimeUtils.resolveDowToDay(base, pt);
   }
  } catch (IllegalFieldValueException ex) {}
  return list;
 } else {
  return null;
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static Partial combineMoreGeneralFields(Partial p1, Partial p2, DateTimeFieldType mgf) {
 Partial p = p1;
 Chronology c1 = p1.getChronology();
 Chronology c2 = p2.getChronology();
 if (!c1.equals(c2)) {
  throw new RuntimeException("Different chronology: c1=" + c1 + ", c2=" + c2);
 if (p1.size() > 0) {
  p1MostGeneralField = p1.getFieldType(0);    // Assume fields ordered from most general to least....
 for (int i = 0; i < p2.size(); i++) {
  DateTimeFieldType fieldType = p2.getFieldType(i);
  if (fieldType == DateTimeFieldType.year()) {
   if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
    if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
     int yoc = p.get(DateTimeFieldType.yearOfCentury());
     int refYear = p2.getValue(i);
     int century = refYear / 100;
     int y2 = yoc + century*100;
     p = p.without(DateTimeFieldType.yearOfCentury());
     p = p.with(DateTimeFieldType.year(), y2);
   } else if (p.isSupported(JodaTimeUtils.DecadeOfCentury)) {
    if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
     int decade = p.get(JodaTimeUtils.DecadeOfCentury);
     int refYear = p2.getValue(i);
     int century = refYear / 100;
     int y2 = decade*10 + century*100;

代码示例来源:origin: stanfordnlp/CoreNLP

if (hasTime) {
 builder.appendLiteral("T");
 if (JodaTimeUtils.hasField(base, DateTimeFieldType.hourOfDay())) {
  builder.appendHourOfDay(2);
 } else if (JodaTimeUtils.hasField(base, DateTimeFieldType.clockhourOfDay())) {
  builder.appendClockhourOfDay(2);
 } else {
  builder.appendLiteral(PAD_FIELD_UNKNOWN2);
 if (JodaTimeUtils.hasField(base, DateTimeFieldType.minuteOfHour())) {
  builder.appendLiteral(":");
  builder.appendMinuteOfHour(2);
 } else if (alwaysPad || JodaTimeUtils.isMoreGeneral(DateTimeFieldType.minuteOfHour(), sdft, base.getChronology())) {
  builder.appendLiteral(":");
  builder.appendLiteral(PAD_FIELD_UNKNOWN2);
  builder.appendLiteral(":");
  builder.appendSecondOfMinute(2);
 } else if (alwaysPad || JodaTimeUtils.isMoreGeneral(DateTimeFieldType.secondOfMinute(), sdft, base.getChronology())) {
  builder.appendLiteral(":");
  builder.appendLiteral(PAD_FIELD_UNKNOWN2);

代码示例来源:origin: joda-time/joda-time

/**
 * Gets a copy of this date with the specified field removed.
 * <p>
 * If this partial did not previously support the field, no error occurs.
 *
 * @param fieldType  the field type to remove, may be null
 * @return a copy of this instance with the field removed
 */
public Partial without(DateTimeFieldType fieldType) {
  int index = indexOf(fieldType);
  if (index != -1) {
    DateTimeFieldType[] newTypes = new DateTimeFieldType[size() - 1];
    int[] newValues = new int[size() - 1];
    System.arraycopy(iTypes, 0, newTypes, 0, index);
    System.arraycopy(iTypes, index + 1, newTypes, index, newTypes.length - index);
    System.arraycopy(iValues, 0, newValues, 0, index);
    System.arraycopy(iValues, index + 1, newValues, index, newValues.length - index);
    Partial newPartial = new Partial(iChronology, newTypes, newValues);
    iChronology.validate(newPartial, newValues);
    return newPartial;
  }
  return this;
}

代码示例来源:origin: joda-time/joda-time

/**
 * Gets a copy of this Partial with the specified period added.
 * <p>
 * If the addition is zero, then <code>this</code> is returned.
 * Fields in the period that aren't present in the partial are ignored.
 * <p>
 * This method is typically used to add multiple copies of complex
 * period instances. Adding one field is best achieved using the method
 * {@link #withFieldAdded(DurationFieldType, int)}.
 * 
 * @param period  the period to add to this one, null means zero
 * @param scalar  the amount of times to add, such as -1 to subtract once
 * @return a copy of this instance with the period added
 * @throws ArithmeticException if the new datetime exceeds the capacity
 */
public Partial withPeriodAdded(ReadablePeriod period, int scalar) {
  if (period == null || scalar == 0) {
    return this;
  }
  int[] newValues = getValues();
  for (int i = 0; i < period.size(); i++) {
    DurationFieldType fieldType = period.getFieldType(i);
    int index = indexOf(fieldType);
    if (index >= 0) {
      newValues = getField(index).add(this, index, newValues,
          FieldUtils.safeMultiply(period.getValue(i), scalar));
    }
  }
  return new Partial(this, newValues);
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static Partial discardMoreSpecificFields(Partial p, DurationFieldType dft)
{
 DurationField df = dft.getField(p.getChronology());
 Partial res = new Partial();
 for (int i = 0; i < p.size(); i++) {
  DateTimeFieldType fieldType = p.getFieldType(i);
  DurationField f = fieldType.getDurationType().getField(p.getChronology());
  int cmp = df.compareTo(f);
  if (cmp <= 0) {
   res = res.with(fieldType, p.getValue(i));
  }
 }
 return res;
}

代码示例来源:origin: joda-time/joda-time

/**
 * Gets a copy of this Partial with the specified field set to a new value.
 * <p>
 * If this partial does not support the field, an exception is thrown.
 * Contrast this behaviour with {@link #with(DateTimeFieldType, int)}.
 * <p>
 * For example, if the field type is <code>dayOfMonth</code> then the day
 * would be changed in the returned instance if supported.
 *
 * @param fieldType  the field type to set, not null
 * @param value  the value to set
 * @return a copy of this instance with the field set
 * @throws IllegalArgumentException if the value is null or invalid
 */
public Partial withField(DateTimeFieldType fieldType, int value) {
  int index = indexOfSupported(fieldType);
  if (value == getValue(index)) {
    return this;
  }
  int[] newValues = getValues();
  newValues = getField(index).set(this, index, newValues, value);
  return new Partial(this, newValues);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Creates a new Partial instance with the specified chronology.
 * This instance is immutable and unaffected by this method call.
 * <p>
 * This method retains the values of the fields, thus the result will
 * typically refer to a different instant.
 * <p>
 * The time zone of the specified chronology is ignored, as Partial
 * operates without a time zone.
 *
 * @param newChronology  the new chronology, null means ISO
 * @return a copy of this datetime with a different chronology
 * @throws IllegalArgumentException if the values are invalid for the new chronology
 */
public Partial withChronologyRetainFields(Chronology newChronology) {
  newChronology = DateTimeUtils.getChronology(newChronology);
  newChronology = newChronology.withUTC();
  if (newChronology == getChronology()) {
    return this;
  } else {
    Partial newPartial = new Partial(newChronology, iTypes, iValues);
    newChronology.validate(newPartial, iValues);
    return newPartial;
  }
}

代码示例来源:origin: joda-time/joda-time

/**
 * Gets a copy of this Partial with the value of the specified field increased.
 * If this partial does not support the field, an exception is thrown.
 * <p>
 * If the addition is zero, then <code>this</code> is returned.
 * The addition will overflow into larger fields (eg. minute to hour).
 * However, it will not wrap around if the top maximum is reached.
 *
 * @param fieldType  the field type to add to, not null
 * @param amount  the amount to add
 * @return a copy of this instance with the field updated
 * @throws IllegalArgumentException if the value is null or invalid
 * @throws ArithmeticException if the new datetime exceeds the capacity
 */
public Partial withFieldAdded(DurationFieldType fieldType, int amount) {
  int index = indexOfSupported(fieldType);
  if (amount == 0) {
    return this;
  }
  int[] newValues = getValues();
  newValues = getField(index).add(this, index, newValues, amount);
  return new Partial(this, newValues);
}

相关文章