use of java.time.ZoneOffset in project graylog2-server by Graylog2.
the class DateTimeConverter method convertToDateTime.
public static DateTime convertToDateTime(@Nonnull Object value) {
if (value instanceof DateTime) {
return (DateTime) value;
}
if (value instanceof Date) {
return new DateTime(value, DateTimeZone.UTC);
} else if (value instanceof ZonedDateTime) {
final DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(((ZonedDateTime) value).getZone()));
return new DateTime(Date.from(((ZonedDateTime) value).toInstant()), dateTimeZone);
} else if (value instanceof OffsetDateTime) {
return new DateTime(Date.from(((OffsetDateTime) value).toInstant()), DateTimeZone.UTC);
} else if (value instanceof LocalDateTime) {
final LocalDateTime localDateTime = (LocalDateTime) value;
final ZoneId defaultZoneId = ZoneId.systemDefault();
final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime);
return new DateTime(Date.from(localDateTime.toInstant(offset)));
} else if (value instanceof LocalDate) {
final LocalDate localDate = (LocalDate) value;
final LocalDateTime localDateTime = localDate.atStartOfDay();
final ZoneId defaultZoneId = ZoneId.systemDefault();
final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime);
return new DateTime(Date.from(localDateTime.toInstant(offset)));
} else if (value instanceof Instant) {
return new DateTime(Date.from((Instant) value), DateTimeZone.UTC);
} else if (value instanceof String) {
return ES_DATE_FORMAT_FORMATTER.parseDateTime((String) value);
} else {
throw new IllegalArgumentException("Value of invalid type <" + value.getClass().getSimpleName() + "> provided");
}
}
use of java.time.ZoneOffset in project CoreNLP by stanfordnlp.
the class SUTime method parseInstant.
/**
* Try parsing a given string into an {@link Instant} in as many ways as we know how.
* Dates will be normalized to the start of their days.
*
* @param value The instant we are parsing.
* @param timezone The timezone, if none is given in the instant.
*
* @return An instant corresponding to the value, if it could be parsed.
*/
public static Optional<java.time.Instant> parseInstant(String value, Optional<ZoneId> timezone) {
for (java.time.format.DateTimeFormatter formatter : DATE_TIME_FORMATS) {
try {
TemporalAccessor datetime = formatter.parse(value);
ZoneId parsedTimezone = datetime.query(TemporalQueries.zoneId());
ZoneOffset parsedOffset = datetime.query(TemporalQueries.offset());
if (parsedTimezone != null) {
return Optional.of(java.time.Instant.from(datetime));
} else if (parsedOffset != null) {
try {
return Optional.of(java.time.Instant.ofEpochSecond(datetime.getLong(ChronoField.INSTANT_SECONDS)));
} catch (UnsupportedTemporalTypeException e) {
return Optional.of(java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(parsedOffset));
}
} else {
if (timezone.isPresent()) {
java.time.Instant reference = java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(ZoneOffset.UTC);
ZoneOffset currentOffsetForMyZone = timezone.get().getRules().getOffset(reference);
try {
return Optional.of(java.time.LocalDateTime.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH), datetime.get(ChronoField.HOUR_OF_DAY), datetime.get(ChronoField.MINUTE_OF_HOUR), datetime.get(ChronoField.SECOND_OF_MINUTE)).toInstant(currentOffsetForMyZone));
} catch (UnsupportedTemporalTypeException e) {
return Optional.of(java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(currentOffsetForMyZone));
}
}
}
} catch (DateTimeParseException ignored) {
}
}
return Optional.empty();
}
use of java.time.ZoneOffset in project sonarqube by SonarSource.
the class GithubAppSecurityImpl method createAppToken.
@Override
public AppToken createAppToken(long appId, String privateKey) {
Algorithm algorithm = readApplicationPrivateKey(appId, privateKey);
LocalDateTime now = LocalDateTime.now(clock);
// Expiration period is configurable and could be greater if needed.
// See https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app
LocalDateTime expiresAt = now.plus(AppToken.EXPIRATION_PERIOD_IN_MINUTES, ChronoUnit.MINUTES);
ZoneOffset offset = clock.getZone().getRules().getOffset(now);
Date nowDate = Date.from(now.toInstant(offset));
Date expiresAtDate = Date.from(expiresAt.toInstant(offset));
JWTCreator.Builder builder = JWT.create().withIssuer(String.valueOf(appId)).withIssuedAt(nowDate).withExpiresAt(expiresAtDate);
return new AppToken(builder.sign(algorithm));
}
use of java.time.ZoneOffset in project drools by kiegroup.
the class TypeUtil method formatValue.
public static String formatValue(final Object val, final boolean wrapForCodeUsage) {
if (val instanceof String) {
return formatString(val.toString(), wrapForCodeUsage);
} else if (val instanceof LocalDate) {
return formatDate((LocalDate) val, wrapForCodeUsage);
} else if (val instanceof LocalTime || val instanceof OffsetTime) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof LocalDateTime || val instanceof OffsetDateTime) {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof ZonedDateTime) {
TemporalAccessor ta = (TemporalAccessor) val;
ZoneId zone = ta.query(TemporalQueries.zone());
if (!(zone instanceof ZoneOffset)) {
// it is a ZoneRegion
return formatDateTimeString(DateAndTimeFunction.REGION_DATETIME_FORMATTER.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
}
} else if (val instanceof Duration) {
return formatDuration((Duration) val, wrapForCodeUsage);
} else if (val instanceof ChronoPeriod) {
return formatPeriod((ChronoPeriod) val, wrapForCodeUsage);
} else if (val instanceof TemporalAccessor) {
TemporalAccessor ta = (TemporalAccessor) val;
if (ta.query(TemporalQueries.localDate()) == null && ta.query(TemporalQueries.localTime()) != null && ta.query(TemporalQueries.zoneId()) != null) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return String.valueOf(val);
}
} else if (val instanceof List) {
return formatList((List) val, wrapForCodeUsage);
} else if (val instanceof Range) {
return formatRange((Range) val, wrapForCodeUsage);
} else if (val instanceof Map) {
return formatContext((Map) val, wrapForCodeUsage);
} else {
return String.valueOf(val);
}
}
use of java.time.ZoneOffset in project tck by dmn-tck.
the class CamundaTCKTest method transformDateTime.
private Object transformDateTime(final XMLGregorianCalendar cal) {
final int nanoSeconds = Optional.ofNullable(cal.getFractionalSecond()).map(fraction -> fraction.movePointRight(9).intValue()).orElse(0);
final int timezone = cal.getTimezone();
final boolean hasOffset = timezone != DatatypeConstants.FIELD_UNDEFINED;
final ZoneOffset offset = hasOffset ? ZoneOffset.ofTotalSeconds(timezone * 60) : null;
final QName type = cal.getXMLSchemaType();
if (type == DatatypeConstants.DATE) {
return LocalDate.of(cal.getYear(), cal.getMonth(), cal.getDay());
}
if (type == DatatypeConstants.TIME) {
final LocalTime localTime = LocalTime.of(cal.getHour(), cal.getMinute(), cal.getSecond(), nanoSeconds);
if (hasOffset) {
return OffsetTime.of(localTime, offset);
} else {
return localTime;
}
}
if (type == DatatypeConstants.DATETIME) {
final LocalDateTime locateDateTime = LocalDateTime.of(cal.getYear(), cal.getMonth(), cal.getDay(), cal.getHour(), cal.getMinute(), cal.getSecond(), nanoSeconds);
if (hasOffset) {
return OffsetDateTime.of(locateDateTime, offset).toZonedDateTime();
} else {
return locateDateTime;
}
}
throw new RuntimeException(String.format("Unexpected calendar value: '%s'", cal));
}
Aggregations