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

x33g5p2x  于2022-01-23 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(129)

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

LocalTime.with介绍

[英]Returns an adjusted copy of this time.

This returns a new LocalTime, based on this one, with the time adjusted. The adjustment takes place using the specified adjuster strategy object. Read the documentation of the adjuster to understand what adjustment will be made.

A simple adjuster might simply set the one of the fields, such as the hour field. A more complex adjuster might set the time to the last hour of the day.

The result of this method is obtained by invoking the TemporalAdjuster#adjustInto(Temporal) method on the specified adjuster passing this as the argument.

This instance is immutable and unaffected by this method call.
[中]返回此时间的调整副本。
这将返回一个新的LocalTime,基于此LocalTime,并调整时间。使用指定的调整器策略对象进行调整。阅读调节器的文档,了解将进行的调整。
一个简单的调整器可以简单地设置其中一个字段,例如小时字段。更复杂的调整器可能会将时间设置为一天中的最后一个小时。
此方法的结果是通过调用指定调整器上的TemporalAdjuster#adjustInto(Temporal)方法获得的,并将其作为参数传递。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

/**
 * Get the ISO 8601 formatted representation of the given {@link java.sql.Time}, which contains time but no date or timezone
 * information.
 * 
 * @param time the JDBC time value; may not be null
 * @param zoneId the timezone identifier or offset where the time is defined
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the ISO 8601 formatted string
 */
public static String toIsoString(java.sql.Time time, ZoneId zoneId, TemporalAdjuster adjuster) {
  LocalTime localTime = time.toLocalTime();
  if (adjuster != null) {
    localTime = localTime.with(adjuster);
  }
  ZonedDateTime zdt = ZonedDateTime.of(Conversions.EPOCH, localTime, zoneId);
  return zdt.format(FORMATTER);
}

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

/**
 * Get the ISO 8601 formatted representation of the given {@link java.sql.Time}, which contains time but no date or timezone
 * information.
 * 
 * @param time the JDBC time value
 * @param zoneId the timezone identifier or offset where the time is defined
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the ISO 8601 formatted string
 */
public static String toIsoString(java.sql.Time time, ZoneId zoneId, TemporalAdjuster adjuster) {
  LocalTime localTime = time.toLocalTime();
  if (adjuster != null) {
    localTime = localTime.with(adjuster);
  }
  ZonedDateTime zdt = ZonedDateTime.of(Conversions.EPOCH, localTime, zoneId);
  return zdt.format(FORMATTER);
}

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

/**
 * Get the number of nanoseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the nanoseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static long toNanoOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster !=null) {
    time = time.with(adjuster);
  }
  return time.toNanoOfDay();
}

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

/**
 * Get the number of milliseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the milliseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static int toMilliOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster != null) {
    time = time.with(adjuster);
  }
  long micros = Math.floorDiv(time.toNanoOfDay(), Conversions.NANOSECONDS_PER_MILLISECOND);
  assert Math.abs(micros) < Integer.MAX_VALUE;
  return (int) micros;
}

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

/**
 * Get the number of microseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the microseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static long toMicroOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster != null) {
    time = time.with(adjuster);
  }
  return Math.floorDiv(time.toNanoOfDay(), Conversions.NANOSECONDS_PER_MICROSECOND);
}

代码示例来源:origin: lesfurets/dOOv

@Override
Function<LocalTime, LocalTime> withFunction(TemporalAdjuster ajuster) {
  return time -> time.with(ajuster);
}

代码示例来源:origin: tec.uom/uom-se

/**
 * Creates the {@link Quantity<Time>} based in the {@link Temporal} with {@link Supplier<TemporalAdjuster>}
 * 
 * @param localTimeA
 * @see {@link LocalTime}
 * @param supplier
 *          he adjust @see {@link TemporalAdjuster}
 * @return The Quantity based in Temporal with TemporalAdjuster in {@link Units#DAY}.
 * @throws java.time.temporal.UnsupportedTemporalTypeException
 *           if some temporal doesn't support {@link ChronoUnit#DAYS}
 */
public static Quantity<Time> getQuantity(LocalTime localTimeA, Supplier<TemporalAdjuster> supplier) {
 LocalTime localTimeB = localTimeA.with(supplier.get());
 return getQuantity(localTimeA, localTimeB);
}

代码示例来源:origin: tec.units/indriya

/**
 * Creates the {@link Quantity<Time>} based in the {@link Temporal} with {@link Supplier<TemporalAdjuster>}
 * 
 * @param localTimeA
 * @see {@link LocalTime}
 * @param supplier
 *          he adjust @see {@link TemporalAdjuster}
 * @return The Quantity based in Temporal with TemporalAdjuster in {@link Units#DAY}.
 * @throws java.time.temporal.UnsupportedTemporalTypeException
 *           if some temporal doesn't support {@link ChronoUnit#DAYS}
 */
public static Quantity<Time> getQuantity(LocalTime localTimeA, Supplier<TemporalAdjuster> supplier) {
 LocalTime localTimeB = localTimeA.with(supplier.get());
 return getQuantity(localTimeA, localTimeB);
}

代码示例来源:origin: io.debezium/debezium-core

/**
 * Get the ISO 8601 formatted representation of the given {@link java.sql.Time}, which contains time but no date or timezone
 * information.
 * 
 * @param time the JDBC time value; may not be null
 * @param zoneId the timezone identifier or offset where the time is defined
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the ISO 8601 formatted string
 */
public static String toIsoString(java.sql.Time time, ZoneId zoneId, TemporalAdjuster adjuster) {
  LocalTime localTime = time.toLocalTime();
  if (adjuster != null) {
    localTime = localTime.with(adjuster);
  }
  ZonedDateTime zdt = ZonedDateTime.of(Conversions.EPOCH, localTime, zoneId);
  return zdt.format(FORMATTER);
}

代码示例来源:origin: io.debezium/debezium-core

/**
 * Get the ISO 8601 formatted representation of the given {@link java.sql.Time}, which contains time but no date or timezone
 * information.
 * 
 * @param time the JDBC time value
 * @param zoneId the timezone identifier or offset where the time is defined
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the ISO 8601 formatted string
 */
public static String toIsoString(java.sql.Time time, ZoneId zoneId, TemporalAdjuster adjuster) {
  LocalTime localTime = time.toLocalTime();
  if (adjuster != null) {
    localTime = localTime.with(adjuster);
  }
  ZonedDateTime zdt = ZonedDateTime.of(Conversions.EPOCH, localTime, zoneId);
  return zdt.format(FORMATTER);
}

代码示例来源:origin: io.debezium/debezium-core

/**
 * Get the number of nanoseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the nanoseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static long toNanoOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster !=null) {
    time = time.with(adjuster);
  }
  return time.toNanoOfDay();
}

代码示例来源:origin: io.debezium/debezium-core

/**
 * Get the number of milliseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the milliseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static int toMilliOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster != null) {
    time = time.with(adjuster);
  }
  long micros = Math.floorDiv(time.toNanoOfDay(), Conversions.NANOSECONDS_PER_MILLISECOND);
  assert Math.abs(micros) < Integer.MAX_VALUE;
  return (int) micros;
}

代码示例来源:origin: io.debezium/debezium-core

/**
 * Get the number of microseconds past midnight of the given {@link java.time.LocalDateTime}, {@link java.time.LocalDate},
 * {@link java.time.LocalTime}, {@link java.util.Date}, {@link java.sql.Date}, {@link java.sql.Time}, or
 * {@link java.sql.Timestamp}, ignoring any date portions of the supplied value.
 * 
 * @param value the local or SQL date, time, or timestamp value; may not be null
 * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no
 * adjustment is necessary
 * @return the microseconds past midnight
 * @throws IllegalArgumentException if the value is not an instance of the acceptable types
 */
public static long toMicroOfDay(Object value, TemporalAdjuster adjuster) {
  LocalTime time = Conversions.toLocalTime(value);
  if (adjuster != null) {
    time = time.with(adjuster);
  }
  return Math.floorDiv(time.toNanoOfDay(), Conversions.NANOSECONDS_PER_MICROSECOND);
}

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

@Override
public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
  if (field instanceof ChronoField) {
    if (field.isTimeBased()) {
      return with(date, time.with(field, newValue));
    } else {
      return with(date.with(field, newValue), time);
    }
  }
  return date.getChronology().ensureChronoLocalDateTime(field.adjustInto(this, newValue));
}

代码示例来源:origin: fbacchella/LogHub

if (ta.isSupported(cf)) {
  logger.trace("{} {}", () -> cf, () -> ta.getLong(cf));
  lt = lt.with(cf, ta.getLong(cf));
  break;
if (ta.isSupported(cf)) {
  logger.trace("{} {}", () -> cf, () -> ta.getLong(cf));
  lt = lt.with(cf, ta.getLong(cf));

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

if (field instanceof ChronoField) {
  if (field.isTimeBased()) {
    return with(date, time.with(field, newValue));
  } else {
    return with(date.with(field, newValue), time);

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

return with(time, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
return with(time.with(field, newValue), offset);

代码示例来源:origin: apache/wicket

@Override
public void convertInput()
{
  Integer hours = hoursField.getConvertedInput();
  Integer minutes = minutesField.getConvertedInput();
  AM_PM amOrPmInput = amOrPmChoice.getConvertedInput();
  LocalTime localTime;
  if (hours == null && minutes == null)
  {
    localTime = null;
  }
  else if (hours != null && minutes != null)
  {
    // Use the input to create a LocalTime object
    localTime = LocalTime.of(hours, minutes);
    // Adjust for halfday if needed
    if (use12HourFormat())
    {
      int halfday = (amOrPmInput == AM_PM.PM ? 1 : 0);
      localTime = localTime.with(ChronoField.AMPM_OF_DAY, halfday);
    }
  }
  else
  {
    error(newValidationError(new ConversionException("Cannot parse time").setTargetType(getType())));
    return;
  }
  setConvertedInput(localTime);
}

相关文章