@Override public Instant convert(OffsetDateTime source) { return source.toInstant(); } }
@Override public Instant convert(OffsetDateTime source) { return source.toInstant(); } }
protected OffsetDateTimeSerializer() { super(OffsetDateTime.class, dt -> dt.toInstant().toEpochMilli(), OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, DateTimeFormatter.ISO_OFFSET_DATE_TIME); }
protected OffsetDateTimeSerializer() { super(OffsetDateTime.class, dt -> dt.toInstant().toEpochMilli(), OffsetDateTime::toEpochSecond, OffsetDateTime::getNano, DateTimeFormatter.ISO_OFFSET_DATE_TIME); }
@Nullable private static Instant getInstant(Object o) { try { if (o instanceof String) { return OffsetDateTime.parse((String) o, TIMESTAMP_PATTERN).toInstant(); } else if (o instanceof Long) { return Instant.ofEpochMilli((Long) o); } } catch (DateTimeException | ClassCastException e) { return null; } return null; } }
/** * @param s string in format {@link #DATETIME_FORMAT} * @throws SonarException when string cannot be parsed */ public static Date parseDateTime(String s) { return Date.from(parseOffsetDateTime(s).toInstant()); }
@Override public Timestamp convertToPersisted(OffsetDateTime value) { if (value == null) { return null; } Instant instant = value.toInstant(); return Timestamp.from(instant); }
@Override public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException { if (value != null) { if (calendar.isPresent()) { // We need to make a clone, because Calendar is not thread-safe // and some JDBC drivers mutate it during time calculations final Calendar calendarClone = (Calendar) calendar.get().clone(); statement.setTimestamp(position, Timestamp.from(value.toInstant()), calendarClone); } else { statement.setTimestamp(position, Timestamp.from(value.toInstant())); } } else { statement.setNull(position, Types.TIMESTAMP); } } }
public static Date toDate(String dateString) { try { return dateParserNoMillis().parse(dateString); } catch (ParseException e) { OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateString); return new Date(offsetDateTime.toInstant().toEpochMilli()); } }
private void setCreatedAfterFromDates(IssueQuery.Builder builder, @Nullable Date createdAfter, @Nullable String createdInLast, boolean createdAfterInclusive) { checkArgument(createdAfter == null || createdInLast == null, format("Parameters %s and %s cannot be set simultaneously", PARAM_CREATED_AFTER, PARAM_CREATED_IN_LAST)); Date actualCreatedAfter = createdAfter; if (createdInLast != null) { actualCreatedAfter = Date.from( OffsetDateTime.now(clock) .minus(Period.parse("P" + createdInLast.toUpperCase(Locale.ENGLISH))) .toInstant()); } builder.createdAfter(actualCreatedAfter, createdAfterInclusive); }
@Override Instant parseInternal(final String text) throws TimestampParseException { final TemporalAccessor temporal; try { temporal = this.formatter.parse(text); } catch (DateTimeParseException ex) { throw new TimestampParseException(ex); } return this.buildOffsetDateTime(temporal).toInstant(); }
JavaTimeArgumentFactory() { register(Instant.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v))); register(LocalDate.class, Types.DATE, (p, i, v) -> p.setDate(i, java.sql.Date.valueOf(v))); register(LocalTime.class, Types.TIME, (p, i, v) -> p.setTime(i, Time.valueOf(v))); register(LocalDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.valueOf(v))); register(OffsetDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant()))); register(ZonedDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant()))); } }
private static long millisUtc(OffsetTime offsetTime) { return offsetTime.atDate(LocalDate.ofEpochDay(0)).toInstant().toEpochMilli(); }
public Clock at( OffsetDateTime datetime ) { return fixed( datetime.toInstant(), datetime.getOffset() ); }
@Override public void logBeforeExecution(StatementContext ctx) { String toString = ctx.getBinding() .findForName(name, ctx) .orElseThrow(AssertionError::new) .toString(); insertedTimestamp = OffsetDateTime.parse(toString); insertedSqlTimestamp = Timestamp.from(insertedTimestamp.toInstant()); }
@Test public void testLegacy() { testLegacyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "%Y-%m-%dT%H:%M:%S %Z", "Asia/Tokyo", "2017-02-28T11:00:45 JST"); }
@Test public void testJava() { testJavaToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "EEE MMM dd HH:mm:ss uuuu XXXXX", "-07:00", "Mon Feb 27 19:00:45 2017 -07:00"); }
@Test public void testRuby() { testRubyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "%Y-%m-%dT%H:%M:%S %Z", "-09:00", "2017-02-27T17:00:45 -09:00"); }
@Test public void testSimpleDateTime() { MapSettings settings = new MapSettings(); settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01T12:13:14+0200"); settings.appendProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version"); Clock clock = mock(Clock.class); ProjectAnalysisInfo info = new ProjectAnalysisInfo(settings.asConfig(), clock); info.start(); OffsetDateTime date = OffsetDateTime.of(2017, 1, 1, 12, 13, 14, 0, ZoneOffset.ofHours(2)); assertThat(info.analysisDate()).isEqualTo(Date.from(date.toInstant())); assertThat(info.analysisVersion()).isEqualTo("version"); }
@Test public void shouldInsertCreatedAndModifiedFields() { Person input = new Person("John", "Phiri"); input.setId(1); recordNextTimestamp("now"); personDAO.insert(input); assertThat(insertedTimestamp.getOffset()).isEqualTo(GMT_PLUS_2); assertThat(insertedTimestamp.toInstant()).isEqualTo(UTC_MOMENT.toInstant()); Person result = personDAO.get(1); assertThat(result.getCreated()) .isEqualTo(result.getModified()) .isEqualTo(insertedSqlTimestamp); }