java.time.OffsetDateTime.now()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(168)

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

OffsetDateTime.now介绍

[英]Obtains the current date-time from the system clock in the default time-zone.

This will query the Clock#systemDefaultZone() in the default time-zone to obtain the current date-time. The offset will be calculated from the time-zone in the clock.

Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
[中]从默认时区的系统时钟获取当前日期时间。
这将查询默认时区中的时钟#systemDefaultZone(),以获取当前日期时间。偏移量将根据时钟中的时区进行计算。
使用此方法将防止使用备用时钟进行测试,因为该时钟是硬编码的。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

  1. @Override
  2. public OffsetDateTime seed(SharedSessionContractImplementor session) {
  3. return OffsetDateTime.now();
  4. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Override
  2. public OffsetDateTime next(OffsetDateTime current, SharedSessionContractImplementor session) {
  3. return OffsetDateTime.now();
  4. }

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

  1. /**
  2. * Reset statistics
  3. */
  4. public void reset() {
  5. statByType.forEach((t, s) ->
  6. s.forEach((k, sh) -> sh.resetStatistics())
  7. );
  8. startTime = OffsetDateTime.now();
  9. }

代码示例来源:origin: SonarSource/sonarqube

  1. private void setCreatedAfterFromDates(IssueQuery.Builder builder, @Nullable Date createdAfter, @Nullable String createdInLast, boolean createdAfterInclusive) {
  2. checkArgument(createdAfter == null || createdInLast == null, format("Parameters %s and %s cannot be set simultaneously", PARAM_CREATED_AFTER, PARAM_CREATED_IN_LAST));
  3. Date actualCreatedAfter = createdAfter;
  4. if (createdInLast != null) {
  5. actualCreatedAfter = Date.from(
  6. OffsetDateTime.now(clock)
  7. .minus(Period.parse("P" + createdInLast.toUpperCase(Locale.ENGLISH)))
  8. .toInstant());
  9. }
  10. builder.createdAfter(actualCreatedAfter, createdAfterInclusive);
  11. }

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. @Override
  2. public Set<EventPublisher> execute(EventPublisherMapper mapper) {
  3. // 取出3秒前已经发送过至队列但是没有收到ack请求的消息,并进行重试
  4. return mapper.selectLimitedEntityByEventStatusBeforeTheSpecifiedUpdateTime(EventStatus.PENDING, 300, OffsetDateTime.now().minusSeconds(3));
  5. }
  6. }

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. @Override
  2. public int updateNonNullProperties(T entity) {
  3. Preconditions.checkNotNull(entity, "entity in updating should not be NULL");
  4. entity.setUpdateTime(OffsetDateTime.now());
  5. return getMapper().updateByPrimaryKeySelective(entity);
  6. }

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. @Override
  2. public int update(T entity) {
  3. Preconditions.checkNotNull(entity, "entity in updating should not be NULL");
  4. entity.setUpdateTime(OffsetDateTime.now());
  5. return getMapper().updateByPrimaryKey(entity);
  6. }

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

  1. @Override
  2. public SqlStatementCustomizer createForMethod(Annotation annotation, Class<?> sqlObjectType, Method method) {
  3. final String parameterName = ((Timestamped) annotation).value();
  4. return stmt -> {
  5. ZoneId zone = stmt.getConfig(TimestampedConfig.class).getTimezone();
  6. stmt.bind(parameterName, OffsetDateTime.now(timeSource.apply(zone)));
  7. };
  8. }

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. /**
  2. * 直接向服务查询, 不再自作聪明地在本地进行过期时间检查, 以免无法区分not found与conflict
  3. */
  4. @Deprecated
  5. private void checkExpireInLocal(TccRequest request, List<Participant> participantLinks) {
  6. // 获取最接近过期的时间
  7. final OffsetDateTime theClosestToExpire = fetchTheRecentlyExpireTime(participantLinks);
  8. if (theClosestToExpire.minusSeconds(LEEWAY).isBefore(OffsetDateTime.now())) {
  9. // 释放全部资源
  10. cancel(request);
  11. throw new ReservationAlmostToExpireException("there are resources be about to expire at " + theClosestToExpire);
  12. }
  13. }

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

  1. private static String currentTimeZoneOffsetString()
  2. {
  3. ZoneOffset offset = OffsetDateTime.now().getOffset();
  4. return offset.equals( UTC ) ? "+0000" : offset.toString().replace( ":", "" );
  5. }
  6. }

代码示例来源:origin: prontera/spring-cloud-rest-tcc

  1. private void initializeOffsetDateTime(T entity) {
  2. entity.setCreateTime(OffsetDateTime.now());
  3. if (entity.getUpdateTime() == null) {
  4. entity.setUpdateTime(IdentityDomain.DEFAULT_DATE_TIME);
  5. }
  6. if (entity.getDeleteTime() == null) {
  7. entity.setDeleteTime(IdentityDomain.DEFAULT_DATE_TIME);
  8. }
  9. }

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

  1. @Override
  2. public SqlStatementCustomizer createForType(Annotation annotation, Class<?> sqlObjectType) {
  3. return stmt -> stmt.bind("now", OffsetDateTime.now(stmt.getConfig(Config.class).clock));
  4. }
  5. }

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

  1. @Test
  2. public void shouldFailWhenOffsetDateTimeIsSentWithRun() throws Exception
  3. {
  4. testFailureWithV2Value( ValueUtils.of( OffsetDateTime.now() ), "OffsetDateTime" );
  5. }

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

  1. @Test
  2. public void shouldCallExternalErrorOnDateTimeWithOffset() throws Exception
  3. {
  4. assumeThat( packerUnderTest.version(), equalTo( 1L ) );
  5. testUnpackableStructParametersWithKnownType( new Neo4jPackV2(), ValueUtils.of( OffsetDateTime.now() ),
  6. "OffsetDateTime values cannot be unpacked with this version of bolt." );
  7. }

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

  1. @Test
  2. public void offsetDateTime() {
  3. OffsetDateTime dt = OffsetDateTime.now();
  4. h.execute("insert into stuff(ts) values (?)", dt);
  5. assertThat(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly()).isEqualTo(dt);
  6. }

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

  1. @Test
  2. public void offsetDateTimeLosesOffset() {
  3. OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.ofHours(-7));
  4. h.execute("insert into stuff(ts) values (?)", dt);
  5. assertThat(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly().isEqual(dt)).isTrue();
  6. }

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

  1. @Test
  2. public void offsetDateTimeLosesOffset() {
  3. OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.ofHours(-7));
  4. h.execute("insert into stuff(ts) values (?)", dt);
  5. assertThat(dt.isEqual(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly())).isTrue();
  6. }

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

  1. @Test
  2. public void offsetDateTime() {
  3. OffsetDateTime dt = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC);
  4. h.execute("insert into stuff(ts) values (?)", dt);
  5. assertThat(h.createQuery("select ts from stuff").mapTo(OffsetDateTime.class).findOnly()).isEqualTo(dt);
  6. }

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

  1. @Test
  2. public void testParseDateAsDuration() {
  3. OffsetDateTime oneMinuteFromNow = OffsetDateTime.now().plusMinutes(1);
  4. long parsedMilliseconds = DateTimeUtils.parseDateAsDuration(oneMinuteFromNow.format(DateTimeFormatter.ISO_DATE_TIME));
  5. assertTrue("Parsed date as duration is bigger than " + MINUTE_IN_MILLISECONDS, parsedMilliseconds <= MINUTE_IN_MILLISECONDS);
  6. assertTrue("Parsed date as duration is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedMilliseconds, parsedMilliseconds > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  7. }

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

  1. @Test
  2. public void testParseRepeatablePeriodAndEndDateTime() {
  3. OffsetDateTime twoMinutesFromNow = OffsetDateTime.now().plusMinutes(2);
  4. String twoMinutesFromNowFormatted = twoMinutesFromNow.format(DateTimeFormatter.ISO_DATE_TIME);
  5. String isoString = "R5/PT1M/" + twoMinutesFromNowFormatted;
  6. long[] parsedRepeatable = DateTimeUtils.parseRepeatableDateTime(isoString);
  7. assertEquals(5L, parsedRepeatable[0]);
  8. assertTrue("Parsed delay is bigger than " + MINUTE_IN_MILLISECONDS, parsedRepeatable[1] <= MINUTE_IN_MILLISECONDS);
  9. assertTrue("Parsed delay is too low! Expected value is between " + MINUTE_IN_MILLISECONDS + " and " + FIFTY_NINE_SECONDS_IN_MILLISECONDS + " but is " + parsedRepeatable[1], parsedRepeatable[1] > FIFTY_NINE_SECONDS_IN_MILLISECONDS);
  10. assertEquals("Parsed period should be one minute in milliseconds but is " + parsedRepeatable[2], MINUTE_IN_MILLISECONDS, parsedRepeatable[2]);
  11. }

相关文章