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

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

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

Temporal.isSupported介绍

[英]Checks if the specified unit is supported.

This checks if the date-time can be queried for the specified unit. If false, then calling the #plus(TemporalAmount) and #minus(TemporalAmount)methods will throw an exception.

Specification for implementors

Implementations must check and handle all fields defined in ChronoUnit. If the field is supported, then true is returned, otherwise false

If the field is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.isSupportedBy(Temporal)passing this as the argument.

Implementations must not alter this object.
[中]检查是否支持指定的单元。
这将检查是否可以查询指定单位的日期时间。如果为false,那么调用#plus(TemporalAmount)和#minus(TemporalAmount)方法将抛出异常。
####实施者规范
实现必须检查并处理IT中定义的所有字段。如果支持该字段,则返回true,否则返回false
如果该字段不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。isSupportedBy(临时)将其作为参数传递。
实现不能改变这个对象。

代码示例

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

@Override
public final boolean isSupported( TemporalField field )
{
  return temporal().isSupported( field );
}

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

@Override
public final boolean isSupported( TemporalUnit unit )
{
  return temporal().isSupported( unit );
}

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

long nanos;
boolean negate = false;
if ( from.isSupported( OFFSET_SECONDS ) && !to.isSupported( OFFSET_SECONDS ) )
int fromNanos = from.isSupported( NANO_OF_SECOND ) ? from.get( NANO_OF_SECOND ) : 0;
int toNanos = to.isSupported( NANO_OF_SECOND ) ? to.get( NANO_OF_SECOND ) : 0;
nanos = toNanos - fromNanos;
    && from.isSupported( SECOND_OF_MINUTE )
    && to.isSupported( SECOND_OF_MINUTE )
    && from.get( SECOND_OF_MINUTE ) != to.get( SECOND_OF_MINUTE );

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

@Override
public Temporal subtractFrom( Temporal temporal )
  if ( months != 0 && temporal.isSupported( MONTHS ) )
  if ( days != 0 && temporal.isSupported( DAYS ) )
    if ( temporal.isSupported( SECONDS ) )
    else if ( temporal.isSupported( DAYS ) )
  if ( nanos != 0 && temporal.isSupported( NANOS ) )

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

@Override
public Temporal addTo( Temporal temporal )
  if ( months != 0 && temporal.isSupported( MONTHS ) )
  if ( days != 0 && temporal.isSupported( DAYS ) )
    if ( temporal.isSupported( SECONDS ) )
  if ( nanos != 0 && temporal.isSupported( NANOS ) )

代码示例来源:origin: kiegroup/optaplanner

@Override
public boolean contains(Temporal_ value) {
  if (value == null || !value.isSupported(incrementUnitType)) {
    return false;
  }
  // We cannot use Temporal.until() to check bounds due to rounding errors
  if (value.compareTo(from) < 0 || value.compareTo(to) >= 0) {
    return false;
  }
  long fromSpace = from.until(value, incrementUnitType);
  if (value.equals(from.plus(fromSpace + 1, incrementUnitType))) {
    // Temporal.until() rounds down, but it needs to round up, to be consistent with Temporal.plus()
    fromSpace++;
  }
  // Only checking the modulus is not enough: 1-MAR + 1 month doesn't include 7-MAR but the modulus is 0 anyway
  return fromSpace % incrementUnitAmount == 0
      && value.equals(from.plus(fromSpace, incrementUnitType));
}

代码示例来源:origin: kiegroup/optaplanner

+ " must have strictly positive incrementUnitAmount (" + incrementUnitAmount + ").");
if (!from.isSupported(incrementUnitType) || !to.isSupported(incrementUnitType)) {
  throw new IllegalArgumentException("The " + getClass().getSimpleName()
      + " must have an incrementUnitType (" + incrementUnitType

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

static DurationValue durationBetween( Temporal from, Temporal to )
{
  long months = 0;
  long days = 0;
  if ( from.isSupported( EPOCH_DAY ) && to.isSupported( EPOCH_DAY ) )
  {
    months = assertValidUntil( from, to, ChronoUnit.MONTHS );
    try
    {
      from = from.plus( months, ChronoUnit.MONTHS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
    days = assertValidUntil( from, to, ChronoUnit.DAYS );
    try
    {
      from = from.plus( days, ChronoUnit.DAYS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
  }
  long nanos = assertValidUntil( from, to, NANOS );
  return newDuration( months, days, nanos / NANOS_PER_SECOND, nanos % NANOS_PER_SECOND );
}

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

/**
 * A utility method that adjusts <a href="https://dev.mysql.com/doc/refman/5.7/en/two-digit-years.html">ambiguous</a> 2-digit
 * year values of DATETIME, DATE, and TIMESTAMP types using these MySQL-specific rules:
 * <ul>
 * <li>Year values in the range 00-69 are converted to 2000-2069.</li>
 * <li>Year values in the range 70-99 are converted to 1970-1999.</li>
 * </ul>
 *
 * @param temporal the temporal instance to adjust; may not be null
 * @return the possibly adjusted temporal instance; never null
 */
protected static Temporal adjustTemporal(Temporal temporal) {
  if (temporal.isSupported(ChronoField.YEAR)) {
    int year = temporal.get(ChronoField.YEAR);
    if (0 <= year && year <= 69) {
      temporal = temporal.plus(2000, ChronoUnit.YEARS);
    } else if (70 <= year && year <= 99) {
      temporal = temporal.plus(1900, ChronoUnit.YEARS);
    }
  }
  return temporal;
}

代码示例来源:origin: com.sonymobile/lumbermill-core

@Override
public boolean isSupportedBy(Temporal temporal) {
  return temporal.isSupported(this);
}

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

@Override
public boolean isSupportedBy(Temporal temporal) {
  return temporal.isSupported(EPOCH_DAY);
}

代码示例来源:origin: com.holon-platform.core/holon-core

/**
 * Get the temporal macro-type of given <code>temporal</code> instance, if available.
 * @param temporal Temporal object
 * @return {@link TemporalType} of given {@link Temporal}, empty if given temporal is <code>null</code>
 */
public static Optional<TemporalType> getTemporalType(Temporal temporal) {
  if (temporal != null) {
    if (temporal.isSupported(ChronoUnit.HOURS)) {
      return Optional.of((temporal.isSupported(ChronoUnit.DAYS)) ? DATE_TIME : TIME);
    }
    return Optional.of(DATE);
  }
  return Optional.empty();
}

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

@Override
public final boolean isSupported( TemporalUnit unit )
{
  return temporal().isSupported( unit );
}

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

@Override
public final boolean isSupported( TemporalField field )
{
  return temporal().isSupported( field );
}

代码示例来源:origin: Silverpeas/Silverpeas-Core

/**
 * @see #toLocalized(Temporal, String)
 * @return only time part if time exists, empty otherwise.
 */
public static String toLocalizedTime(final Temporal temporal, final String language) {
 Objects.requireNonNull(temporal);
 Objects.requireNonNull(language);
 final String result;
 if (temporal.isSupported(ChronoUnit.HOURS)) {
  result = DateTimeFormatter.ofPattern(getTimePattern(language)).format(temporal);
 } else {
  result = "";
 }
 return result;
}

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

long nanos;
boolean negate = false;
if ( from.isSupported( OFFSET_SECONDS ) && !to.isSupported( OFFSET_SECONDS ) )
int fromNanos = from.isSupported( NANO_OF_SECOND ) ? from.get( NANO_OF_SECOND ) : 0;
int toNanos = to.isSupported( NANO_OF_SECOND ) ? to.get( NANO_OF_SECOND ) : 0;
nanos = toNanos - fromNanos;
    && from.isSupported( SECOND_OF_MINUTE )
    && to.isSupported( SECOND_OF_MINUTE )
    && from.get( SECOND_OF_MINUTE ) != to.get( SECOND_OF_MINUTE );

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

private static final long millis(Temporal temporal) {
  // java.sql.* temporal types:
  if (temporal instanceof LocalDate) {
    return Date.valueOf((LocalDate) temporal).getTime();
  }
  else if (temporal instanceof LocalTime) {
    return Time.valueOf((LocalTime) temporal).getTime();
  }
  else if (temporal instanceof LocalDateTime) {
    return Timestamp.valueOf((LocalDateTime) temporal).getTime();
  }
  // OffsetDateTime
  else if (temporal.isSupported(INSTANT_SECONDS)) {
    return 1000 * temporal.getLong(INSTANT_SECONDS) + temporal.getLong(MILLI_OF_SECOND);
  }
  // OffsetTime
  else if (temporal.isSupported(MILLI_OF_DAY)) {
    return temporal.getLong(MILLI_OF_DAY);
  }
  throw fail(temporal, Long.class);
}

代码示例来源:origin: com.nhl.link.rest/link-rest-base

@Override
protected T valueNonNull(JsonNode node) {
  Temporal temporal = ISODateParser.parser().fromString(node.asText());
  GregorianCalendar calendar = new GregorianCalendar();
  calendar.setTimeInMillis(0);
  ZoneId zone = temporal.query(TemporalQueries.zone());
  if (zone != null) {
    calendar.setTimeZone(TimeZone.getTimeZone(zone));
  }
  if (temporal.isSupported(ChronoField.YEAR)) {
    int year = temporal.get(ChronoField.YEAR);
    int monthOfYear = temporal.get(ChronoField.MONTH_OF_YEAR);
    int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH);
    calendar.set(year, --monthOfYear, dayOfMonth);
  }
  if (temporal.isSupported(ChronoField.HOUR_OF_DAY)) {
    int hours = temporal.get(ChronoField.HOUR_OF_DAY);
    int minutes = temporal.get(ChronoField.MINUTE_OF_HOUR);
    int seconds = temporal.get(ChronoField.SECOND_OF_MINUTE);
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    calendar.set(Calendar.MINUTE, minutes);
    calendar.set(Calendar.SECOND, seconds);
  }
  if (temporal.isSupported(ChronoField.MILLI_OF_SECOND)) {
    int millis = temporal.get(ChronoField.MILLI_OF_SECOND);
    calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
  }
  return normalizer.apply(calendar.getTime());
}

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

static DurationValue durationBetween( Temporal from, Temporal to )
{
  long months = 0;
  long days = 0;
  if ( from.isSupported( EPOCH_DAY ) && to.isSupported( EPOCH_DAY ) )
  {
    months = assertValidUntil( from, to, ChronoUnit.MONTHS );
    try
    {
      from = from.plus( months, ChronoUnit.MONTHS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
    days = assertValidUntil( from, to, ChronoUnit.DAYS );
    try
    {
      from = from.plus( days, ChronoUnit.DAYS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
  }
  long nanos = assertValidUntil( from, to, NANOS );
  return newDuration( months, days, nanos / NANOS_PER_SECOND, nanos % NANOS_PER_SECOND );
}

代码示例来源:origin: Silverpeas/Silverpeas-Core

/**
 * @see #toLocalized(Temporal, ZoneId, String)
 * @return only time part if time exists, empty otherwise.
 */
public static String toLocalizedTime(final Temporal temporal, final ZoneId zoneId,
  final String language) {
 if (temporal.isSupported(ChronoUnit.HOURS) && temporal.get(ChronoField.OFFSET_SECONDS) !=
   ZonedDateTime.now(zoneId).get(ChronoField.OFFSET_SECONDS)) {
  final ZoneId actualZoneId = asZonedDateTime(temporal).getZone();
  final String pattern = getTimePattern(language);
  return toZonedFormat(pattern, temporal, actualZoneId);
 } else {
  return toLocalizedTime(temporal, language);
 }
}

相关文章