java.time.temporal.Temporal.with()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(274)

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

Temporal.with介绍

[英]Returns an adjusted object of the same type as this object with the adjustment made.

This adjusts this date-time according to the rules of the specified adjuster. A simple adjuster might simply set the one of the fields, such as the year field. A more complex adjuster might set the date to the last day of the month. A selection of common adjustments is provided in TemporalAdjusters. These include finding the "last day of the month" and "next Wednesday". The adjuster is responsible for handling special cases, such as the varying lengths of month and leap years.

Some example code indicating how and why this method is used:

date = date.with(Month.JULY);        // most key classes implement TemporalAdjuster 
date = date.with(lastDayOfMonth());  // static import from TemporalAdjusters 
date = date.with(next(WEDNESDAY));   // static import from TemporalAdjusters and DayOfWeek

Specification for implementors

Implementations must not alter either this object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
[中]返回与此对象类型相同的已调整对象,并进行调整。
这将根据指定调整器的规则调整此日期时间。一个简单的调整器可以简单地设置其中一个字段,例如年份字段。更复杂的调整器可能会将日期设置为当月的最后一天。临时调整器中提供了一些常用调整。其中包括查找“本月最后一天”和“下周三”。理算师负责处理特殊情况,例如不同长度的月份和闰年。
一些示例代码说明了该方法的使用方式和原因:

date = date.with(Month.JULY);        // most key classes implement TemporalAdjuster 
date = date.with(lastDayOfMonth());  // static import from TemporalAdjusters 
date = date.with(next(WEDNESDAY));   // static import from TemporalAdjusters and DayOfWeek

####实施者规范
实现不能改变这两个对象。相反,必须返回经调整的原件副本。这为不可变和可变的实现提供了等效的、安全的行为。

代码示例

代码示例来源:origin: neo4j/neo4j

@SuppressWarnings( "unchecked" )
@Override
public <R extends Temporal> R adjustInto( R temporal, long newValue )
{
  int newVal = range.checkValidIntValue( newValue, this );
  int oldYear = temporal.get( ChronoField.YEAR );
  return (R) temporal.with( ChronoField.YEAR, (oldYear / years) * years + newVal )
            .with( TemporalAdjusters.firstDayOfYear() );
}

代码示例来源:origin: neo4j/neo4j

@Override
@SuppressWarnings( "unchecked" )
public final V with( TemporalField field, long newValue )
{
  return replacement( (T) temporal().with( field, newValue ) );
}

代码示例来源:origin: neo4j/neo4j

@Override
@SuppressWarnings( "unchecked" )
public final V with( TemporalAdjuster adjuster )
{
  return replacement( (T) temporal().with( adjuster ) );
}

代码示例来源:origin: neo4j/neo4j

result = (Temp) result.with( IsoFields.WEEK_BASED_YEAR,
      safeCastIntegral( f.name(), entry.getValue(), f.defaultValue ) );
      .with( temporalField, safeCastIntegral( f.name(), entry.getValue(), f.defaultValue ) );
 fields.containsKey( TemporalFields.microsecond ) || fields.containsKey( TemporalFields.nanosecond )) )
result = (Temp) result.with( TemporalFields.nanosecond.field,
    validNano( fields.get( TemporalFields.millisecond ), fields.get( TemporalFields.microsecond ),
        fields.get( TemporalFields.nanosecond ) ) );

代码示例来源:origin: neo4j/neo4j

TEMP newTemporal = (TEMP) temporal.with( ChronoField.NANO_OF_SECOND, newNanos );
MapValue filtered = fields.filter(
    ( k, ignore ) -> !k.equals( "microsecond" ) && !k.equals( "nanosecond" ) );
AnyValue nanos = fields.get( "nanosecond" );
int newNanos = validNano( null, micros, nanos );
TEMP newTemporal = (TEMP) temporal.with( ChronoField.NANO_OF_SECOND, newNanos );
MapValue filtered = fields.filter(
    ( k, ignore ) -> !k.equals( "nanosecond" ) );

代码示例来源:origin: eclipse/smarthome

@Nullable
Temporal isOk(Temporal t) {
  if (checker.matches(t)) {
    return null;
  }
  Temporal out = t.plus(1, type.getBaseUnit());
  // Fall-through switch case. for example if type is year all cases below must also be handled.
  switch (type) {
    case YEAR:
      out = out.with(ChronoField.MONTH_OF_YEAR, 1);
    case MONTH_OF_YEAR:
      out = out.with(ChronoField.DAY_OF_MONTH, 1);
    case DAY_OF_WEEK:
    case DAY_OF_MONTH:
      out = out.with(ChronoField.HOUR_OF_DAY, 0);
    case HOUR_OF_DAY:
      out = out.with(ChronoField.MINUTE_OF_HOUR, 0);
    case MINUTE_OF_HOUR:
      out = out.with(ChronoField.SECOND_OF_MINUTE, 0);
    case SECOND_OF_MINUTE:
      return out;
    default:
      throw new IllegalArgumentException("Invalid field type " + type);
  }
}

代码示例来源:origin: dremio/dremio-oss

@Override
 public Temporal adjustInto(Temporal temporal) {
  Temporal result = temporal;
  for(TemporalAdjuster adjuster: copyOf) {
   result = result.with(adjuster);
  }
  return result;
 }
};

代码示例来源:origin: optimatika/ojAlgo

@SuppressWarnings("unchecked")
public <T extends Temporal> T adjustInto(final T temporal) {
  if (temporal instanceof CalendarDate) {
    return (T) this;
  } else {
    final long seconds = millis / MILLIS_PER_SECOND;
    final long nanos = (millis % MILLIS_PER_SECOND) * (NANOS_PER_SECOND / MILLIS_PER_SECOND);
    return (T) temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
  }
}

代码示例来源:origin: org.ojalgo/ojalgo

@SuppressWarnings("unchecked")
public <T extends Temporal> T adjustInto(final T temporal) {
  if (temporal instanceof CalendarDate) {
    return (T) this;
  } else {
    final long seconds = millis / MILLIS_PER_SECOND;
    final long nanos = (millis % MILLIS_PER_SECOND) * (NANOS_PER_SECOND / MILLIS_PER_SECOND);
    return (T) temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
  }
}

代码示例来源:origin: dremio/dremio-oss

@Override
 public Temporal adjustInto(Temporal temporal) {
  Temporal adjusted = temporal.with(adjuster);
  return (ChronoUnit.NANOS.between(temporal, adjusted) >= 0)
    ? adjusted
    : temporal.plus(amount).with(adjuster);
  }
};

代码示例来源:origin: org.threeten/threeten-extra

@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
  long hour = newValue / 100;
  long min = newValue % 100;
  HOUR_OF_DAY.checkValidValue(hour);
  MINUTE_OF_HOUR.checkValidValue(min);
  return (R) temporal.with(HOUR_OF_DAY, hour).with(MINUTE_OF_HOUR, min);
}

代码示例来源:origin: org.neo4j/neo4j-values

@SuppressWarnings( "unchecked" )
@Override
public <R extends Temporal> R adjustInto( R temporal, long newValue )
{
  int newVal = range.checkValidIntValue( newValue, this );
  int oldYear = temporal.get( ChronoField.YEAR );
  return (R) temporal.with( ChronoField.YEAR, (oldYear / years) * years + newVal )
            .with( TemporalAdjusters.firstDayOfYear() );
}

代码示例来源:origin: dremio/dremio-oss

@Override
 public Temporal adjustInto(Temporal temporal) {
  long adjustedDayOfMonth = Math.min(dayOfMonth, temporal.range(ChronoField.DAY_OF_MONTH).getMaximum());
  return temporal.with(ChronoField.DAY_OF_MONTH, adjustedDayOfMonth);
 }
};

代码示例来源:origin: owlike/genson

protected T readFieldFromObject(T obj, ObjectReader reader){
    Temporal objTemporal = (Temporal) obj;
    String jsonName = reader.name();
    TemporalField field = temporalFields.get(jsonName);
    long value = reader.valueAsLong();
    return (T) objTemporal.with(field, value);
  }
}

代码示例来源:origin: org.neo4j/neo4j-values

@Override
@SuppressWarnings( "unchecked" )
public final V with( TemporalField field, long newValue )
{
  return replacement( (T) temporal().with( field, newValue ) );
}

代码示例来源:origin: org.neo4j/neo4j-values

@Override
@SuppressWarnings( "unchecked" )
public final V with( TemporalAdjuster adjuster )
{
  return replacement( (T) temporal().with( adjuster ) );
}

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
public Temporal adjustInto(Temporal temporal) {
  return temporal
      .with(EPOCH_DAY, toLocalDate().toEpochDay())
      .with(NANO_OF_DAY, toLocalTime().toNanoOfDay());
}

代码示例来源:origin: com.github.seratch/java-time-backport

@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
  long curValue = getFrom(temporal);
  range().checkValidValue(newValue, this);
  return (R) temporal.with(DAY_OF_YEAR, temporal.getLong(DAY_OF_YEAR) + (newValue - curValue));
}
@Override

代码示例来源:origin: com.github.seratch/java-time-backport

@SuppressWarnings("unchecked")
  @Override
  public <R extends Temporal> R adjustInto(R temporal, long newValue) {
    long curValue = getFrom(temporal);
    range().checkValidValue(newValue, this);
    return (R) temporal.with(MONTH_OF_YEAR, temporal.getLong(MONTH_OF_YEAR) + (newValue - curValue) * 3);
  }
},

代码示例来源:origin: org.omnifaces/omniutils

public static TemporalAdjuster nextOrSameDayOfMonth(int dayOfMonth) {
  validateDayOfMonth(dayOfMonth);
  TemporalAdjuster nextDayOfMonth = nextDayOfMonth(dayOfMonth);
  return temporal -> {
    int currentDayOfMonth = temporal.get(DAY_OF_MONTH);
    if (currentDayOfMonth == dayOfMonth || (currentDayOfMonth < dayOfMonth && currentDayOfMonth == temporal.range(DAY_OF_MONTH).getMaximum())) {
      return temporal;
    }
    return temporal.with(nextDayOfMonth);
  };
}

相关文章