Java Code Examples for java.time.Instant#from()

The following examples show how to use java.time.Instant#from() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   File: InstantFormatter.java    License: MIT License 5 votes vote down vote up
@Override
public Instant parse(String text, Locale locale) throws ParseException {
	if (text.length() > 0 && Character.isDigit(text.charAt(0))) {
		// assuming UTC instant a la "2007-12-03T10:15:30.00Z"
		return Instant.parse(text);
	}
	else {
		// assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
		return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
	}
}
 
Example 2
private static WrappedMetadata getWrappedMetadata(Map<String, ?> wrapInfo, VaultToken token) {

		TemporalAccessor creation_time = getDate(wrapInfo, "creation_time");
		String path = (String) wrapInfo.get("creation_path");
		Duration ttl = getTtl(wrapInfo);

		return new WrappedMetadata(token, ttl, Instant.from(creation_time), path);
	}
 
Example 3
Source Project: fess   File: FavoriteLogBhv.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected LocalDateTime toLocalDateTime(final Object value) {
    if (value != null) {
        try {
            final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
            final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            return date;
        } catch (final DateTimeParseException e) {
            logger.debug("Invalid date format: {}", value, e);
        }
    }
    return DfTypeUtil.toLocalDateTime(value);
}
 
Example 4
private static Instant parseTime(Config params, String key)
{
    try {
        return Instant.ofEpochSecond(
                params.get(key, long.class)
        );
    }
    catch (ConfigException ex) {
        return Instant.from(
                TIME_PARSER
                        .withZone(params.get("timezone", ZoneId.class))
                        .parse(params.get(key, String.class))
        );
    }
}
 
Example 5
Source Project: fess   File: ClickLogBhv.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected LocalDateTime toLocalDateTime(final Object value) {
    if (value != null) {
        try {
            final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
            final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            return date;
        } catch (final DateTimeParseException e) {
            logger.debug("Invalid date format: {}", value, e);
        }
    }
    return DfTypeUtil.toLocalDateTime(value);
}
 
Example 6
/**
 * Creates a new instance.
 *
 * @param fqdn a fully qualified domain name
 * @param notBefore {@link Certificate} is not valid before this time
 * @param notAfter {@link Certificate} is not valid after this time
 */
public SelfSignedCertificateRuleDelegate(String fqdn,
                                         TemporalAccessor notBefore, TemporalAccessor notAfter) {
    this.fqdn = requireNonNull(fqdn, "fqdn");
    random = null;
    bits = null;
    this.notBefore = Instant.from(requireNonNull(notBefore, "notBefore"));
    this.notAfter = Instant.from(requireNonNull(notAfter, "notAfter"));
}
 
Example 7
Source Project: gcp-ingestion   File: Time.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Attempts to parse a string in format '2011-12-03T10:15:30Z', returning null in case of error.
 */
public static Instant parseAsInstantOrNull(String timestamp) {
  try {
    return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(timestamp));
  } catch (DateTimeParseException | NullPointerException ignore) {
    return null;
  }
}
 
Example 8
Source Project: centraldogma   File: UserAndTimestamp.java    License: Apache License 2.0 5 votes vote down vote up
@JsonCreator
UserAndTimestamp(@JsonProperty("user") String user,
                 @JsonProperty("timestamp") String timestampAsText) {
    this.user = requireNonNull(user, "user");
    this.timestampAsText = requireNonNull(timestampAsText, "timestampAsText");
    timestamp = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(timestampAsText));
}
 
Example 9
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 10
Source Project: phoebus   File: DateTimePane.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/** @return Time currently shown in pane */
public Instant getInstant()
{
    final LocalDateTime local = LocalDateTime.of(date.getValue(), LocalTime.of(hour.getValue(), minute.getValue(), second.getValue()));
    return Instant.from(local.atZone(ZoneId.systemDefault()));
}
 
Example 11
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 12
Source Project: styx   File: ParameterUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static Instant parseDate(String date) {
  return Instant.from(LocalDate.from(
      DateTimeFormatter.ISO_LOCAL_DATE.parse(date))
                          .atStartOfDay(UTC));
}
 
Example 13
Source Project: digdag   File: DailySchedulerTest.java    License: Apache License 2.0 4 votes vote down vote up
static Instant instant(String time)
{
    return Instant.from(TIME_FORMAT.parse(time));
}
 
Example 14
Source Project: Bytecoder   File: Chronology.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 15
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 16
Source Project: hottub   File: Chronology.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 17
/**
 * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
 * <p>
 * This obtains a zoned date-time in this chronology based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ChronoZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code ChronoLocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * The result uses this chronology.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 * @see ChronoZonedDateTime#from(TemporalAccessor)
 */
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
            return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example 18
@Override
public Date unmarshal(String v) throws Exception {
    TemporalAccessor dateTime = MCRISO8601FormatChooser.getFormatter(v, null).parse(v);
    Instant instant = Instant.from(dateTime);
    return Date.from(instant);
}
 
Example 19
private Instant parseSigningDate(String signatureDate) {
    return Instant.from(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'")
                                         .withZone(ZoneId.of("UTC"))
                                         .parse(signatureDate));
}
 
Example 20
@Nullable
private static Instant toInstant(String date) {

	return StringUtils.hasText(date) ? Instant.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(date)) : null;
}