java.time.ZonedDateTime.minusHours()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(178)

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

ZonedDateTime.minusHours介绍

[英]Returns a copy of this ZonedDateTime with the specified period in hours subtracted.

This operates on the instant time-line, such that subtracting one hour will always be a duration of one hour earlier. This may cause the local date-time to change by an amount other than one hour. Note that this is a different approach to that used by days, months and years, thus subtracting one day is not the same as adding 24 hours.

For example, consider a time-zone where the spring DST cutover means that the local times 01:00 to 01:59 occur twice changing from offset +02:00 to +01:00.

  • Subtracting one hour from 02:30+01:00 will result in 01:30+02:00
  • Subtracting one hour from 01:30+01:00 will result in 01:30+02:00
  • Subtracting one hour from 01:30+02:00 will result in 00:30+01:00
  • Subtracting three hours from 02:30+01:00 will result in 00:30+02:00

This instance is immutable and unaffected by this method call.
[中]返回此ZoneDateTime的副本,并减去指定的时间段(以小时为单位)。
这是在即时时间线上进行的,因此减去一个小时的持续时间总是提前一个小时。这可能会导致本地日期时间的更改量超过一小时。请注意,这与按天、月和年使用的方法不同,因此减去一天与加24小时并不相同。
例如,考虑一个时区,其中Spring DST切换意味着本地时间01:00到01:59发生两次从偏移+02:00到+01:00的变化。
*从02:30+01:00减去一小时将得到01:30+02:00
*从01:30+01:00减去一小时将得到01:30+02:00
*从01:30+02:00减去一小时将得到00:30+01:00
*从02:30+01:00减去三小时将得到00:30+02:00
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: spring-cloud/spring-cloud-gateway

static String minusHours(int hours) {
  return ZonedDateTime.now().minusHours(hours).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}

代码示例来源:origin: spring-cloud/spring-cloud-gateway

@Test
public void testPredicates() {
  boolean result = new AfterRoutePredicateFactory().apply(c -> c.setDatetime(ZonedDateTime.now().minusHours(2))).test(getExchange());
  assertThat(result).isTrue();
}

代码示例来源:origin: spring-cloud/spring-cloud-gateway

@Test
public void testPredicates() {
  boolean result = new BeforeRoutePredicateFactory().apply(c -> c.setDatetime(ZonedDateTime.now().minusHours(2))).test(getExchange());
  assertThat(result).isFalse();
}

代码示例来源:origin: spring-cloud/spring-cloud-gateway

@Test
public void testPredicates() {
  boolean result = new BetweenRoutePredicateFactory()
      .apply(c -> c.setDatetime1(ZonedDateTime.now().minusHours(2))
          .setDatetime2(ZonedDateTime.now().plusHours(1)))
      .test(getExchange());
  assertThat(result).isTrue();
}

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

public ZonedDateTime minusHours(long amount) {
  return dt.minusHours(amount);
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

public ZonedDateTime minusHours(long amount) {
  return dt.minusHours(amount);
}

代码示例来源:origin: apache/servicemix-bundles

public ZonedDateTime minusHours(long amount) {
  return dt.minusHours(amount);
}

代码示例来源:origin: stackoverflow.com

ZonedDateTime zoned_0330_minus_1H = zoned_0330.minusHours( 1 );
ZonedDateTime zoned_0330_minus_2H = zoned_0330.minusHours( 2 );

代码示例来源:origin: no.api.meteo/meteo-core

private List<HourMatcher> createIndexKeysFromPeriodForecast(PeriodForecast periodForecast) {
  List<HourMatcher> keyList = new ArrayList<>();
  ZonedDateTime from = cloneZonedDateTime(periodForecast.getFrom());
  ZonedDateTime activeTime = cloneZonedDateTime(periodForecast.getTo());
  while (activeTime.isAfter(from)) {
    keyList.add(createHourIndexKey(activeTime));
    activeTime = activeTime.minusHours(1);
  }
  keyList.add(createHourIndexKey(from));
  return keyList;
}

代码示例来源:origin: no.api.meteo/meteo-core

/**
 * Create a detailed forecast for a given date within this location forecast.
 *
 * The periods chosen is based on the Norwegian periods (0-6, 6-12, 12-18 and 18-00)
 *
 * @param dateTime
 *         The date to create the forecast for.
 *
 * @return The detailed forecast for the given date. Will be empty if data is not found.
 */
public MeteoExtrasForecastDay createForcastForDay(ZonedDateTime dateTime) {
  ZonedDateTime dt = toZeroHMSN(dateTime.withZoneSameInstant(zoneId));
  log.error("Create for : " + dt.toString());
  List<MeteoExtrasForecast> forecasts = new ArrayList<>();
  findBestForecastForPeriod(dt.minusHours(2),
               dt.plusHours(4)).ifPresent(forecasts::add);
  findBestForecastForPeriod(dt.plusHours(4),
               dt.plusHours(10)).ifPresent(forecasts::add);
  findBestForecastForPeriod(dt.plusHours(10),
               dt.plusHours(16)).ifPresent(forecasts::add);
  findBestForecastForPeriod(dt.plusHours(16),
               dt.plusHours(22)).ifPresent(forecasts::add);
  return new MeteoExtrasForecastDay(dt.toLocalDate(), forecasts);
}

代码示例来源:origin: otto-de/edison-microservice

@Override
  public List<OAuthPublicKey> retrieveActivePublicKeys() {
    final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    final String verifierKey = "-----BEGIN PUBLIC KEY-----\n" +
        Base64.getEncoder().encodeToString(publicKey.getEncoded()) +
        "\n-----END PUBLIC KEY-----";
    final OAuthPublicKey oAuthPublicKey = OAuthPublicKey
        .oAuthPublicKeyBuilder()
        .withPublicKey(verifierKey)
        .withPublicKeyFingerprint("someFingerprint")
        .withValidFrom(now().minusHours(1))
        .build();
    return Collections.singletonList(oAuthPublicKey);
  }
};

代码示例来源:origin: xetys/jhipster-uaa-setup

@Test
public void assertThatResetKeyMustBeValid() {
  User user = userService.createUser("johndoe", "johndoe", "John", "Doe", "john.doe@localhost", "en-US");
  ZonedDateTime daysAgo = ZonedDateTime.now().minusHours(25);
  user.setActivated(true);
  user.setResetDate(daysAgo);
  user.setResetKey("1234");
  userRepository.save(user);
  Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
  assertThat(maybeUser.isPresent()).isFalse();
  userRepository.delete(user);
}

代码示例来源:origin: xetys/jhipster-uaa-setup

@Test
public void assertThatResetKeyMustNotBeOlderThan24Hours() {
  User user = userService.createUser("johndoe", "johndoe", "John", "Doe", "john.doe@localhost", "en-US");
  ZonedDateTime daysAgo = ZonedDateTime.now().minusHours(25);
  String resetKey = RandomUtil.generateResetKey();
  user.setActivated(true);
  user.setResetDate(daysAgo);
  user.setResetKey(resetKey);
  userRepository.save(user);
  Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
  assertThat(maybeUser.isPresent()).isFalse();
  userRepository.delete(user);
}

代码示例来源:origin: xetys/jhipster-uaa-setup

@Test
public void assertThatUserCanResetPassword() {
  User user = userService.createUser("johndoe", "johndoe", "John", "Doe", "john.doe@localhost", "en-US");
  String oldPassword = user.getPassword();
  ZonedDateTime daysAgo = ZonedDateTime.now().minusHours(2);
  String resetKey = RandomUtil.generateResetKey();
  user.setActivated(true);
  user.setResetDate(daysAgo);
  user.setResetKey(resetKey);
  userRepository.save(user);
  Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
  assertThat(maybeUser.isPresent()).isTrue();
  assertThat(maybeUser.get().getResetDate()).isNull();
  assertThat(maybeUser.get().getResetKey()).isNull();
  assertThat(maybeUser.get().getPassword()).isNotEqualTo(oldPassword);
  userRepository.delete(user);
}

代码示例来源:origin: apache/incubator-rya

@Test
public void testHours() throws DatatypeConfigurationException, ValueExprEvaluationException {
  DatatypeFactory dtf = DatatypeFactory.newInstance();
  ZonedDateTime zTime = testThisTimeDate;
  String time = zTime.format(DateTimeFormatter.ISO_INSTANT);
  ZonedDateTime zTime1 = zTime.minusHours(1);
  String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
  Literal now = VF.createLiteral(dtf.newXMLGregorianCalendar(time));
  Literal nowMinusOne = VF.createLiteral(dtf.newXMLGregorianCalendar(time1));
  DateTimeWithinPeriod func = new DateTimeWithinPeriod();
  assertEquals(TRUE, func.evaluate(VF, now, now,VF.createLiteral(1),OWLTime.HOURS_URI));
  assertEquals(FALSE, func.evaluate(VF, now, nowMinusOne,VF.createLiteral(1),OWLTime.HOURS_URI));
  assertEquals(TRUE, func.evaluate(VF, now, nowMinusOne,VF.createLiteral(2),OWLTime.HOURS_URI));
}

代码示例来源:origin: SonarSource/sonarlint-intellij

@Test
public void testSerialization() {
 SonarLintProjectState state = new SonarLintProjectState();
 state.setLastEventPolling(ZonedDateTime.now().minusHours(2));
 SonarLintProjectState copy = state.getState();
 assertThat(copy.getLastEventPolling()).isEqualTo(state.getLastEventPolling());
 SonarLintProjectState loaded = new SonarLintProjectState();
 loaded.loadState(state);
 assertThat(loaded.getLastEventPolling()).isEqualTo(state.getLastEventPolling());
}

代码示例来源:origin: org.meridor.perspective/perspective-mock

public static Image getImage() {
  Image image = new Image();
  image.setId("test-image");
  image.setRealId("test-image");
  image.setProjectId(getProject().getId());
  image.setName("test-image");
  image.setState(ImageState.SAVED);
  image.setCreated(now().minusDays(2));
  image.setTimestamp(now().minusHours(4));
  MetadataMap metadata = new MetadataMap();
  image.setMetadata(metadata);
  return image;
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private static CartDiscountDraftBuilder newCartDiscountDraftBuilder(final String predicate) {
  final ZonedDateTime validFrom = ZonedDateTime.now().minusHours(1);
  final ZonedDateTime validUntil = validFrom.plusSeconds(8000);
  final LocalizedString name = en("discount name");
  final LocalizedString description = en("discount descriptions");
  final AbsoluteCartDiscountValue value = CartDiscountValue.ofAbsolute(MoneyImpl.of(10, EUR));
  final LineItemsTarget target = LineItemsTarget.of("1 = 1");
  final String sortOrder = randomSortOrder();
  final boolean requiresDiscountCode = false;
  return CartDiscountDraftBuilder.of(name, CartPredicate.of(predicate),
      value, target, sortOrder, requiresDiscountCode)
      .validFrom(validFrom)
      .validUntil(validUntil)
      .description(description);
}

代码示例来源:origin: UniversaBlockchain/universa

@Test
public void recordExpiration() throws Exception {
  // todo: expired can't be get - it should be dropped by the database
  HashId hashId = HashId.createRandom();
  StateRecord r = ledger.findOrCreate(hashId);
  long recordId = r.getRecordId();
  ZonedDateTime inFuture = ZonedDateTime.now().plusHours(2);
  r.setExpiresAt(inFuture);
  StateRecord r1 = ledger.getRecord(hashId);
  assertNotEquals(r1.getExpiresAt(), inFuture);
  r.save();
  r1 = ledger.getRecord(hashId);
  assertAlmostSame(r.getExpiresAt(), r1.getExpiresAt());
  r.setExpiresAt(ZonedDateTime.now().minusHours(1));
  r.save();
  r1 = ledger.getRecord(hashId);
  assertNull(r1);
}

代码示例来源:origin: UniversaBlockchain/universa

@Test
public void recordExpiration() throws Exception {
  // todo: expired can't be get - it should be dropped by the database
  HashId hashId = HashId.createRandom();
  StateRecord r = ledger.findOrCreate(hashId);
  assertNotNull(r.getExpiresAt());
  assert(r.getExpiresAt().isAfter(ZonedDateTime.now()));
  long recordId = r.getRecordId();
  ZonedDateTime inFuture = ZonedDateTime.now().plusHours(2);
  r.setExpiresAt(inFuture);
  StateRecord r1 = ledger.getRecord(hashId);
  assertNotEquals(r1.getExpiresAt(), inFuture);
  r.save();
  r1 = ledger.getRecord(hashId);
  assertAlmostSame(r.getExpiresAt(), r1.getExpiresAt());
  r.setExpiresAt(ZonedDateTime.now().minusHours(1));
  r.save();
  r1 = ledger.getRecord(hashId);
  assertNull(r1);
}

相关文章

ZonedDateTime类方法