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

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

本文整理了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

  1. /**
  2. * Returns an {@link java.time.OffsetDateTime} with the time portion cleared.
  3. *
  4. * @param self an OffsetDateTime
  5. * @return an OffsetDateTime
  6. * @since 2.5.0
  7. */
  8. public static OffsetDateTime clearTime(final OffsetDateTime self) {
  9. return self.truncatedTo(DAYS);
  10. }

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

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

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

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

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

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

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

  1. /**
  2. * 時刻情報を切り捨てます
  3. *
  4. * @param date
  5. * 日付
  6. * @return 時刻情報を切り捨てた日付
  7. */
  8. public static OffsetDateTime truncateTime(final OffsetDateTime date) {
  9. if (date == null) {
  10. return null;
  11. }
  12. return date.truncatedTo(ChronoUnit.DAYS);
  13. }

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

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

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

  1. /**
  2. * ミリ秒以下を切り捨てます
  3. *
  4. * @param date
  5. * 日付
  6. * @return 時刻情報を切り捨てた日付
  7. */
  8. public static OffsetDateTime truncateMilisecond(final OffsetDateTime date) {
  9. if (date == null) {
  10. return null;
  11. }
  12. return date.truncatedTo(ChronoUnit.SECONDS);
  13. }

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

  1. private JobInfo(final String jobId,
  2. final String jobType,
  3. final OffsetDateTime started,
  4. final OffsetDateTime lastUpdated,
  5. final Optional<OffsetDateTime> stopped,
  6. final JobStatus status,
  7. final List<JobMessage> messages,
  8. Clock clock, final String hostname) {
  9. this.jobId = jobId;
  10. this.jobType = jobType;
  11. //Truncate to milliseconds precision because current persistence implementations only support milliseconds
  12. this.started = started != null ? started.truncatedTo(ChronoUnit.MILLIS) : null;
  13. this.lastUpdated = lastUpdated != null ? lastUpdated.truncatedTo(ChronoUnit.MILLIS) : null;
  14. this.stopped = stopped.map(offsetDateTime -> offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
  15. this.status = status;
  16. this.messages = unmodifiableList(messages);
  17. this.hostname = hostname;
  18. this.clock = clock;
  19. }

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

  1. private JobInfo(final String jobType, final String jobId, final Clock clock, final String hostname) {
  2. this.jobId = jobId;
  3. this.jobType = jobType;
  4. //Truncate to milliseconds precision because current persistence implementations only support milliseconds
  5. this.started = now(clock).truncatedTo(ChronoUnit.MILLIS);
  6. this.clock = clock;
  7. this.stopped = empty();
  8. this.status = OK;
  9. this.lastUpdated = started;
  10. this.hostname = hostname;
  11. this.messages = emptyList();
  12. }

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

  1. timeBucket = dateTime.truncatedTo(MILLIS).toInstant().toEpochMilli();
  2. break;
  3. case SECOND:
  4. timeBucket = dateTime.truncatedTo(SECONDS).toInstant().toEpochMilli();
  5. break;
  6. case MINUTE:
  7. timeBucket = dateTime.truncatedTo(MINUTES).toInstant().toEpochMilli();
  8. break;
  9. case HOUR:
  10. timeBucket = dateTime.truncatedTo(HOURS).toInstant().toEpochMilli();
  11. break;
  12. case DAY:
  13. timeBucket = dateTime.truncatedTo(DAYS).toInstant().toEpochMilli();
  14. break;
  15. case WEEK:
  16. timeBucket = dateTime.with(firstDayOfWeek()).truncatedTo(DAYS).toInstant().toEpochMilli();
  17. break;
  18. case MONTH:
  19. timeBucket = dateTime.with(firstDayOfMonth()).truncatedTo(DAYS).toInstant().toEpochMilli();
  20. break;
  21. case YEAR:
  22. timeBucket = dateTime.with(firstDayOfYear()).truncatedTo(DAYS).toInstant().toEpochMilli();
  23. break;
  24. default:

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

  1. @Test
  2. public void shouldAppendMessageToJobInfo() throws Exception {
  3. String someUri = "someUri";
  4. OffsetDateTime now = now();
  5. //Given
  6. JobInfo jobInfo = newJobInfo(someUri, "TEST", systemDefaultZone(), "localhost");
  7. repository.createOrUpdate(jobInfo);
  8. //When
  9. JobMessage igelMessage = JobMessage.jobMessage(Level.WARNING, "Der Igel ist froh.", now);
  10. repository.appendMessage(someUri, igelMessage);
  11. //Then
  12. JobInfo jobInfoFromRepo = repository.findOne(someUri).get();
  13. assertThat(jobInfoFromRepo.getMessages().size(), is(1));
  14. assertThat(jobInfoFromRepo.getMessages().get(0), is(igelMessage));
  15. assertThat(jobInfoFromRepo.getLastUpdated(), is(now.truncatedTo(ChronoUnit.MILLIS)));
  16. }

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

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

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

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

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

  1. @Test
  2. public void testObjectToOffsetDateTime() throws Exception {
  3. ZoneId zoneId = ZoneId.systemDefault();
  4. OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneId);
  5. testObjectToOffsetDateTime(null, null);
  6. testObjectToOffsetDateTime(offsetDateTime, offsetDateTime);
  7. testObjectToOffsetDateTime(offsetDateTime.toLocalDateTime(), offsetDateTime);
  8. testObjectToOffsetDateTime(offsetDateTime.toInstant(), offsetDateTime);
  9. testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  10. testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
  11. testObjectToOffsetDateTime(offsetDateTime.toLocalDate(), offsetDateTime.truncatedTo(ChronoUnit.DAYS));
  12. testObjectToOffsetDateTime(Date.from(offsetDateTime.toInstant()), offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
  13. try {
  14. testObjectToOffsetDateTime("a string", offsetDateTime);
  15. fail();
  16. } catch (IllegalArgumentException e) {
  17. // expected
  18. }
  19. }

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

  1. @Test
  2. public void shouldReturnJobIfJobExists() throws Exception {
  3. // given
  4. ZoneId cet = ZoneId.of("CET");
  5. OffsetDateTime now = OffsetDateTime.now(cet).truncatedTo(ChronoUnit.MILLIS);
  6. JobInfo expectedJob = newJobInfo("42", "TEST", fixed(now.toInstant(), cet), "localhost");
  7. when(jobService.findJob("42")).thenReturn(Optional.of(expectedJob));
  8. String nowAsString = ISO_OFFSET_DATE_TIME.format(now);
  9. mockMvc.perform(MockMvcRequestBuilders
  10. .get("/some-microservice/internal/jobs/42")
  11. .servletPath("/internal/jobs/42"))
  12. .andExpect(status().is(200))
  13. .andExpect(jsonPath("$.status").value("OK"))
  14. .andExpect(jsonPath("$.messages").isArray())
  15. .andExpect(jsonPath("$.jobType").value("TEST"))
  16. .andExpect(jsonPath("$.hostname").value("localhost"))
  17. .andExpect(jsonPath("$.started").value(nowAsString))
  18. .andExpect(jsonPath("$.stopped").value(""))
  19. .andExpect(jsonPath("$.lastUpdated").value(nowAsString))
  20. .andExpect(jsonPath("$.jobUri").value("http://localhost/some-microservice/internal/jobs/42"))
  21. .andExpect(jsonPath("$.links").isArray())
  22. .andExpect(jsonPath("$.links[0].href").value("http://localhost/some-microservice/internal/jobs/42"))
  23. .andExpect(jsonPath("$.links[1].href").value("http://localhost/some-microservice/internal/jobdefinitions/TEST"))
  24. .andExpect(jsonPath("$.links[2].href").value("http://localhost/some-microservice/internal/jobs"))
  25. .andExpect(jsonPath("$.links[3].href").value("http://localhost/some-microservice/internal/jobs?type=TEST"))
  26. .andExpect(jsonPath("$.runtime").value("00:00:00"))
  27. .andExpect(jsonPath("$.state").value("Running"));
  28. verify(jobService).findJob("42");
  29. }

相关文章