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

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

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

OffsetDateTime.toInstant介绍

[英]Converts this date-time to an Instant.
[中]将此日期时间转换为瞬间。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. @Override
  2. public Instant convert(OffsetDateTime source) {
  3. return source.toInstant();
  4. }
  5. }

代码示例来源:origin: org.springframework/spring-context

  1. @Override
  2. public Instant convert(OffsetDateTime source) {
  3. return source.toInstant();
  4. }
  5. }

代码示例来源:origin: prestodb/presto

  1. protected OffsetDateTimeSerializer() {
  2. super(OffsetDateTime.class, dt -> dt.toInstant().toEpochMilli(),
  3. OffsetDateTime::toEpochSecond, OffsetDateTime::getNano,
  4. DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  5. }

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

  1. protected OffsetDateTimeSerializer() {
  2. super(OffsetDateTime.class, dt -> dt.toInstant().toEpochMilli(),
  3. OffsetDateTime::toEpochSecond, OffsetDateTime::getNano,
  4. DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  5. }

代码示例来源:origin: codecentric/spring-boot-admin

  1. @Nullable
  2. private static Instant getInstant(Object o) {
  3. try {
  4. if (o instanceof String) {
  5. return OffsetDateTime.parse((String) o, TIMESTAMP_PATTERN).toInstant();
  6. } else if (o instanceof Long) {
  7. return Instant.ofEpochMilli((Long) o);
  8. }
  9. } catch (DateTimeException | ClassCastException e) {
  10. return null;
  11. }
  12. return null;
  13. }
  14. }

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

  1. /**
  2. * @param s string in format {@link #DATETIME_FORMAT}
  3. * @throws SonarException when string cannot be parsed
  4. */
  5. public static Date parseDateTime(String s) {
  6. return Date.from(parseOffsetDateTime(s).toInstant());
  7. }

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

  1. @Override
  2. public Timestamp convertToPersisted(OffsetDateTime value) {
  3. if (value == null) {
  4. return null;
  5. }
  6. Instant instant = value.toInstant();
  7. return Timestamp.from(instant);
  8. }

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

  1. @Override
  2. public void apply(final int position,
  3. final PreparedStatement statement,
  4. final StatementContext ctx) throws SQLException {
  5. if (value != null) {
  6. if (calendar.isPresent()) {
  7. // We need to make a clone, because Calendar is not thread-safe
  8. // and some JDBC drivers mutate it during time calculations
  9. final Calendar calendarClone = (Calendar) calendar.get().clone();
  10. statement.setTimestamp(position, Timestamp.from(value.toInstant()), calendarClone);
  11. } else {
  12. statement.setTimestamp(position, Timestamp.from(value.toInstant()));
  13. }
  14. } else {
  15. statement.setNull(position, Types.TIMESTAMP);
  16. }
  17. }
  18. }

代码示例来源:origin: knowm/XChange

  1. public static Date toDate(String dateString) {
  2. try {
  3. return dateParserNoMillis().parse(dateString);
  4. } catch (ParseException e) {
  5. OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateString);
  6. return new Date(offsetDateTime.toInstant().toEpochMilli());
  7. }
  8. }

代码示例来源: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: embulk/embulk

  1. @Override
  2. Instant parseInternal(final String text) throws TimestampParseException {
  3. final TemporalAccessor temporal;
  4. try {
  5. temporal = this.formatter.parse(text);
  6. } catch (DateTimeParseException ex) {
  7. throw new TimestampParseException(ex);
  8. }
  9. return this.buildOffsetDateTime(temporal).toInstant();
  10. }

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

  1. JavaTimeArgumentFactory() {
  2. register(Instant.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v)));
  3. register(LocalDate.class, Types.DATE, (p, i, v) -> p.setDate(i, java.sql.Date.valueOf(v)));
  4. register(LocalTime.class, Types.TIME, (p, i, v) -> p.setTime(i, Time.valueOf(v)));
  5. register(LocalDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.valueOf(v)));
  6. register(OffsetDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant())));
  7. register(ZonedDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant())));
  8. }
  9. }

代码示例来源:origin: prestodb/presto

  1. private static long millisUtc(OffsetTime offsetTime)
  2. {
  3. return offsetTime.atDate(LocalDate.ofEpochDay(0)).toInstant().toEpochMilli();
  4. }

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

  1. public Clock at( OffsetDateTime datetime )
  2. {
  3. return fixed( datetime.toInstant(), datetime.getOffset() );
  4. }

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

  1. @Override
  2. public void logBeforeExecution(StatementContext ctx) {
  3. String toString = ctx.getBinding()
  4. .findForName(name, ctx)
  5. .orElseThrow(AssertionError::new)
  6. .toString();
  7. insertedTimestamp = OffsetDateTime.parse(toString);
  8. insertedSqlTimestamp = Timestamp.from(insertedTimestamp.toInstant());
  9. }

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

  1. @Test
  2. public void testLegacy() {
  3. testLegacyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(),
  4. "%Y-%m-%dT%H:%M:%S %Z",
  5. "Asia/Tokyo",
  6. "2017-02-28T11:00:45 JST");
  7. }

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

  1. @Test
  2. public void testJava() {
  3. testJavaToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(),
  4. "EEE MMM dd HH:mm:ss uuuu XXXXX",
  5. "-07:00",
  6. "Mon Feb 27 19:00:45 2017 -07:00");
  7. }

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

  1. @Test
  2. public void testRuby() {
  3. testRubyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(),
  4. "%Y-%m-%dT%H:%M:%S %Z",
  5. "-09:00",
  6. "2017-02-27T17:00:45 -09:00");
  7. }

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

  1. @Test
  2. public void testSimpleDateTime() {
  3. MapSettings settings = new MapSettings();
  4. settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01T12:13:14+0200");
  5. settings.appendProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version");
  6. Clock clock = mock(Clock.class);
  7. ProjectAnalysisInfo info = new ProjectAnalysisInfo(settings.asConfig(), clock);
  8. info.start();
  9. OffsetDateTime date = OffsetDateTime.of(2017, 1, 1, 12, 13, 14, 0, ZoneOffset.ofHours(2));
  10. assertThat(info.analysisDate()).isEqualTo(Date.from(date.toInstant()));
  11. assertThat(info.analysisVersion()).isEqualTo("version");
  12. }

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

  1. @Test
  2. public void shouldInsertCreatedAndModifiedFields() {
  3. Person input = new Person("John", "Phiri");
  4. input.setId(1);
  5. recordNextTimestamp("now");
  6. personDAO.insert(input);
  7. assertThat(insertedTimestamp.getOffset()).isEqualTo(GMT_PLUS_2);
  8. assertThat(insertedTimestamp.toInstant()).isEqualTo(UTC_MOMENT.toInstant());
  9. Person result = personDAO.get(1);
  10. assertThat(result.getCreated())
  11. .isEqualTo(result.getModified())
  12. .isEqualTo(insertedSqlTimestamp);
  13. }

相关文章