java.time.Instant.get()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(177)

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

Instant.get介绍

[英]Gets the value of the specified field from this instant as an int.

This queries this instant for the value for the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.

If the field is a ChronoField then the query is implemented here. The #isSupported(TemporalField) will return valid values based on this date-time, except INSTANT_SECONDS which is too large to fit in an int and throws a DateTimeException. All other ChronoField instances will throw a DateTimeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor)passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.
[中]从该瞬间获取指定字段的int值。
这将立即查询指定字段的值。返回的值将始终在字段的有效值范围内。如果由于字段不受支持或其他原因而无法返回值,则会引发异常。
如果该字段是一个ChronoField,则在此处实现查询。#isSupported(TemporalField)将基于此日期时间返回有效值,但INSTANT_SECONDS除外,该值太大,无法容纳int并引发DateTimeException。所有其他ChronoField实例将引发DateTimeException。
如果字段不是ChronoField,则通过调用TemporalField获得此方法的结果。getFrom(临时Accessor)将此作为参数传递。是否可以获得该值以及该值表示的内容由字段决定。

代码示例

代码示例来源:origin: EvoSuite/evosuite

public static int get(Instant instant, TemporalField field) {
  return instant.get(field);
}

代码示例来源:origin: org.apache.wss4j/wss4j-ws-security-dom

int milliseconds = created.get(ChronoField.MILLI_OF_SECOND);
if (milliseconds * 1000000 != created.getNano()) {
  bspEnforcer.handleBSPRule(BSPRule.R3220);
int milliseconds = expires.get(ChronoField.MILLI_OF_SECOND);
if (milliseconds * 1000000 != expires.getNano()) {
  bspEnforcer.handleBSPRule(BSPRule.R3229);

代码示例来源:origin: com.centurylink.mdw/mdw-common

public static String dateToString(Date d) {
  if (d == null)
    return null;
  if (d.toInstant().get(ChronoField.MICRO_OF_SECOND) > 0)
    return new SimpleDateFormat(_dateFormatMs).format(d);
  else
    return new SimpleDateFormat(_dateFormat).format(d);
}

代码示例来源:origin: org.apache.wss4j/wss4j-ws-security-stax

private void detectReplayAttack(InputProcessorChain inputProcessorChain) throws WSSecurityException {
  TimestampSecurityEvent timestampSecurityEvent =
      inputProcessorChain.getSecurityContext().get(WSSConstants.PROP_TIMESTAMP_SECURITYEVENT);
  ReplayCache replayCache =
    ((WSSSecurityProperties)getSecurityProperties()).getTimestampReplayCache();
  if (timestampSecurityEvent != null && replayCache != null) {
    final String cacheKey =
        timestampSecurityEvent.getCreated().get(ChronoField.MILLI_OF_SECOND)
        + "" + Arrays.hashCode(getSignatureType().getSignatureValue().getValue());
    if (replayCache.contains(cacheKey)) {
      throw new WSSecurityException(WSSecurityException.ErrorCode.MESSAGE_EXPIRED);
    }
    // Store the Timestamp/SignatureValue combination in the cache
    Instant expires = timestampSecurityEvent.getExpires();
    if (expires != null) {
      Instant currentTime = Instant.now();
      replayCache.add(cacheKey, 1L + Duration.between(currentTime, expires).getSeconds());
    } else {
      replayCache.add(cacheKey);
    }
  }
}

相关文章