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

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

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

OffsetDateTime.truncatedTo介绍

[英]Returns a copy of this OffsetDateTime with the time truncated.

Truncation returns a copy of the original date-time with fields smaller than the specified unit set to zero. For example, truncating with the ChronoUnit#MINUTES unit will set the second-of-minute and nano-of-second field to zero.

The unit must have a TemporalUnit#getDuration()that divides into the length of a standard day without remainder. This includes all supplied time units on ChronoUnit and ChronoUnit#DAYS. Other units throw an exception.

The offset does not affect the calculation and will be the same in the result.

This instance is immutable and unaffected by this method call.
[中]

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * Returns an {@link java.time.OffsetDateTime} with the time portion cleared.
 *
 * @param self an OffsetDateTime
 * @return an OffsetDateTime
 * @since 2.5.0
 */
public static OffsetDateTime clearTime(final OffsetDateTime self) {
  return self.truncatedTo(DAYS);
}

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

private JobMessage(final Level level, final String message, final OffsetDateTime timestamp) {
  this.level = level;
  this.message = message;
  //Truncate to milliseconds precision because current persistence implementations only support milliseconds
  this.timestamp = timestamp != null ? timestamp.truncatedTo(ChronoUnit.MILLIS) : null;
}

代码示例来源:origin: net.aholbrook.paseto/core

public Token setExpiration(OffsetDateTime expiration) {
  this.exp = expiration;
  // Cut off mills/nanos. The formatter does this too, but only after output.
  if (this.exp != null) {
    this.exp = this.exp.truncatedTo(ChronoUnit.SECONDS);
  }
  return this;
}

代码示例来源:origin: net.aholbrook.paseto/core

public Token setIssuedAt(OffsetDateTime issuedAt) {
  this.iat = issuedAt;
  // Cut off mills/nanos. The formatter does this too, but only after output.
  if (this.iat != null) {
    this.iat = this.iat.truncatedTo(ChronoUnit.SECONDS);
  }
  return this;
}

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

/**
 * 時刻情報を切り捨てます
 * 
 * @param date
 *            日付
 * @return 時刻情報を切り捨てた日付
 */
public static OffsetDateTime truncateTime(final OffsetDateTime date) {
  if (date == null) {
    return null;
  }
  return date.truncatedTo(ChronoUnit.DAYS);
}

代码示例来源:origin: net.aholbrook.paseto/core

public Token setNotBefore(OffsetDateTime notBefore) {
  this.nbf = notBefore;
  // Cut off mills/nanos. The formatter does this too, but only after output.
  if (this.nbf != null) {
    this.nbf = this.nbf.truncatedTo(ChronoUnit.SECONDS);
  }
  return this;
}

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

/**
 * ミリ秒以下を切り捨てます
 * 
 * @param date
 *            日付
 * @return 時刻情報を切り捨てた日付
 */
public static OffsetDateTime truncateMilisecond(final OffsetDateTime date) {
  if (date == null) {
    return null;
  }
  return date.truncatedTo(ChronoUnit.SECONDS);
}

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

private JobInfo(final String jobId,
        final String jobType,
        final OffsetDateTime started,
        final OffsetDateTime lastUpdated,
        final Optional<OffsetDateTime> stopped,
        final JobStatus status,
        final List<JobMessage> messages,
        Clock clock, final String hostname) {
  this.jobId = jobId;
  this.jobType = jobType;
  //Truncate to milliseconds precision because current persistence implementations only support milliseconds
  this.started = started != null ? started.truncatedTo(ChronoUnit.MILLIS) : null;
  this.lastUpdated = lastUpdated != null ? lastUpdated.truncatedTo(ChronoUnit.MILLIS) : null;
  this.stopped = stopped.map(offsetDateTime -> offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
  this.status = status;
  this.messages = unmodifiableList(messages);
  this.hostname = hostname;
  this.clock = clock;
}

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

private JobInfo(final String jobType, final String jobId, final Clock clock, final String hostname) {
  this.jobId = jobId;
  this.jobType = jobType;
  //Truncate to milliseconds precision because current persistence implementations only support milliseconds
  this.started = now(clock).truncatedTo(ChronoUnit.MILLIS);
  this.clock = clock;
  this.stopped = empty();
  this.status = OK;
  this.lastUpdated = started;
  this.hostname = hostname;
  this.messages = emptyList();
}

代码示例来源:origin: uk.gov.gchq.gaffer/common-util

timeBucket = dateTime.truncatedTo(MILLIS).toInstant().toEpochMilli();
  break;
case SECOND:
  timeBucket = dateTime.truncatedTo(SECONDS).toInstant().toEpochMilli();
  break;
case MINUTE:
  timeBucket = dateTime.truncatedTo(MINUTES).toInstant().toEpochMilli();
  break;
case HOUR:
  timeBucket = dateTime.truncatedTo(HOURS).toInstant().toEpochMilli();
  break;
case DAY:
  timeBucket = dateTime.truncatedTo(DAYS).toInstant().toEpochMilli();
  break;
case WEEK:
  timeBucket = dateTime.with(firstDayOfWeek()).truncatedTo(DAYS).toInstant().toEpochMilli();
  break;
case MONTH:
  timeBucket = dateTime.with(firstDayOfMonth()).truncatedTo(DAYS).toInstant().toEpochMilli();
  break;
case YEAR:
  timeBucket = dateTime.with(firstDayOfYear()).truncatedTo(DAYS).toInstant().toEpochMilli();
  break;
default:

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

@Test
public void shouldAppendMessageToJobInfo() throws Exception {
  String someUri = "someUri";
  OffsetDateTime now = now();
  //Given
  JobInfo jobInfo = newJobInfo(someUri, "TEST", systemDefaultZone(), "localhost");
  repository.createOrUpdate(jobInfo);
  //When
  JobMessage igelMessage = JobMessage.jobMessage(Level.WARNING, "Der Igel ist froh.", now);
  repository.appendMessage(someUri, igelMessage);
  //Then
  JobInfo jobInfoFromRepo = repository.findOne(someUri).get();
  assertThat(jobInfoFromRepo.getMessages().size(), is(1));
  assertThat(jobInfoFromRepo.getMessages().get(0), is(igelMessage));
  assertThat(jobInfoFromRepo.getLastUpdated(), is(now.truncatedTo(ChronoUnit.MILLIS)));
}

代码示例来源:origin: Azure/azure-storage-java

if (identifier.accessPolicy() != null && identifier.accessPolicy().start() != null) {
  identifier.accessPolicy().withStart(
      identifier.accessPolicy().start().truncatedTo(ChronoUnit.SECONDS));
      identifier.accessPolicy().expiry().truncatedTo(ChronoUnit.SECONDS));

代码示例来源:origin: com.microsoft.azure/azure-storage-blob

if (identifier.accessPolicy() != null && identifier.accessPolicy().start() != null) {
  identifier.accessPolicy().withStart(
      identifier.accessPolicy().start().truncatedTo(ChronoUnit.SECONDS));
      identifier.accessPolicy().expiry().truncatedTo(ChronoUnit.SECONDS));

代码示例来源:origin: arnaudroger/SimpleFlatMapper

@Test
public void testObjectToOffsetDateTime() throws Exception {
  ZoneId zoneId = ZoneId.systemDefault();
  OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneId);
  testObjectToOffsetDateTime(null, null);
  testObjectToOffsetDateTime(offsetDateTime, offsetDateTime);
  testObjectToOffsetDateTime(offsetDateTime.toLocalDateTime(), offsetDateTime);
  testObjectToOffsetDateTime(offsetDateTime.toInstant(), offsetDateTime);
  testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  testObjectToOffsetDateTime(offsetDateTime.toLocalDate(), offsetDateTime.truncatedTo(ChronoUnit.DAYS));
  testObjectToOffsetDateTime(Date.from(offsetDateTime.toInstant()), offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
  try {
    testObjectToOffsetDateTime("a string", offsetDateTime);
    fail();
  } catch (IllegalArgumentException e) {
    // expected
  }
}

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

@Test
public void shouldReturnJobIfJobExists() throws Exception {
  // given
  ZoneId cet = ZoneId.of("CET");
  OffsetDateTime now = OffsetDateTime.now(cet).truncatedTo(ChronoUnit.MILLIS);
  JobInfo expectedJob = newJobInfo("42", "TEST", fixed(now.toInstant(), cet), "localhost");
  when(jobService.findJob("42")).thenReturn(Optional.of(expectedJob));
  String nowAsString = ISO_OFFSET_DATE_TIME.format(now);
  mockMvc.perform(MockMvcRequestBuilders
      .get("/some-microservice/internal/jobs/42")
      .servletPath("/internal/jobs/42"))
      .andExpect(status().is(200))
      .andExpect(jsonPath("$.status").value("OK"))
      .andExpect(jsonPath("$.messages").isArray())
      .andExpect(jsonPath("$.jobType").value("TEST"))
      .andExpect(jsonPath("$.hostname").value("localhost"))
      .andExpect(jsonPath("$.started").value(nowAsString))
      .andExpect(jsonPath("$.stopped").value(""))
      .andExpect(jsonPath("$.lastUpdated").value(nowAsString))
      .andExpect(jsonPath("$.jobUri").value("http://localhost/some-microservice/internal/jobs/42"))
      .andExpect(jsonPath("$.links").isArray())
      .andExpect(jsonPath("$.links[0].href").value("http://localhost/some-microservice/internal/jobs/42"))
      .andExpect(jsonPath("$.links[1].href").value("http://localhost/some-microservice/internal/jobdefinitions/TEST"))
      .andExpect(jsonPath("$.links[2].href").value("http://localhost/some-microservice/internal/jobs"))
      .andExpect(jsonPath("$.links[3].href").value("http://localhost/some-microservice/internal/jobs?type=TEST"))
      .andExpect(jsonPath("$.runtime").value("00:00:00"))
      .andExpect(jsonPath("$.state").value("Running"));
  verify(jobService).findJob("42");
}

相关文章