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

The following are Jave code examples for showing how to use from() of the java.time.Instant class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: fernet-java8   File: Client.java   Source Code and License Vote up 6 votes
/**
 * This presents a form to the end user based on the state of the application. If the customer had previously filled
 * out a form, a pre-filled form will be presented.
 * 
 * @return either a blank or autofilled form
 */
public Form renderForm() {
    if (insecureStorage.containsKey("secureEnvelope") && insecureStorage.containsKey("expirationDateTime")) {
        final String expirationDateTimeString = insecureStorage.get("expirationDateTime");
        final Instant expirationDateTime = Instant.from(formatter.parse(expirationDateTimeString));
        if (Instant.now(clock).isBefore(expirationDateTime)) {
            // a non-expired token is available, present a pre-filled form
            final Form autoFillForm = new Form();
            // present the obfuscated data from insecure storage
            autoFillForm.firstName = insecureStorage.get("firstName");
            autoFillForm.lastName = insecureStorage.get("lastName");
            autoFillForm.emailAddress = insecureStorage.get("emailAddress");
            autoFillForm.autofilled = true;
            return autoFillForm;
        }
        // we've cached the sensitive information, but the token is expired and unusable
        // clear the cache
        insecureStorage.clear();
    }
    // either there is no stored data or it is expired, so render a blank form
    return new Form();
}
 
Example 2
Project: CalendarFX   File: VirtualGrid.java   Source Code and License Vote up 6 votes
/**
 * Adjusts the given instant either rounding it up or down.
 *
 * @param instant
 *            the instant to adjust
 * @param zoneId
 *            the time zone
 * @param roundUp
 *            the rounding direction
 * @param firstDayOfWeek
 *            the first day of the week (needed for rounding weeks)
 * @return the adjusted instant
 */
public Instant adjustTime(Instant instant, ZoneId zoneId, boolean roundUp,
                          DayOfWeek firstDayOfWeek) {

    requireNonNull(instant);
    requireNonNull(zoneId);
    requireNonNull(firstDayOfWeek);

    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
    if (roundUp) {
        zonedDateTime = zonedDateTime.plus(getAmount(), getUnit());
    }

    zonedDateTime = Util.truncate(zonedDateTime, getUnit(), getAmount(),
            firstDayOfWeek);

    return Instant.from(zonedDateTime);
}
 
Example 3
Project: quilt   File: OerGeneralizedTimeCodec.java   Source Code and License Vote up 6 votes
@Override
public OerGeneralizedTime read(CodecContext context, InputStream inputStream) throws IOException {
  Objects.requireNonNull(context);
  Objects.requireNonNull(inputStream);

  final String timeString = context.read(OerIA5String.class, inputStream).getValue();

  if (timeString.length() != 19 || !timeString.endsWith("Z")) {
    throw new IllegalArgumentException(
        "Interledger GeneralizedTime only supports values in the format 'YYYYMMDDTHHMMSS.fffZ',"
            + " value " + timeString + " is invalid.");
  }

  try {
    final Instant value = Instant.from(generalizedTimeFormatter.parse(timeString));
    return new OerGeneralizedTime(value);
  } catch (DateTimeParseException dtp) {
    throw new IllegalArgumentException(
        "Interledger GeneralizedTime only supports values in the format 'YYYYMMDDTHHMMSS.fffZ', "
            + "value " + timeString + " is invalid.",
        dtp);
  }
}
 
Example 4
Project: optashift-employee-rostering   File: Chrono.java   Source Code and License Vote up 6 votes
/**
 * Creates a zoned date-time in this chronology from another date-time object.
 * <p>
 * This creates a date-time in this chronology based on the specified {@code DateTimeAccessor}.
 * <p>
 * This should obtain a {@code ZoneId} using {@link ZoneId#from(DateTimeAccessor)}. The date-time should be
 * obtained by obtaining an {@code Instant}. If that fails, the local date-time should be used.
 * 
 * @param dateTime the date-time object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 */
public ChronoZonedDateTime<C> zonedDateTime(DateTimeAccessor dateTime) {

  try {
    ZoneId zoneId = ZoneId.from(dateTime);
    ChronoDateTimeImpl<C> cldt;
    try {
      Instant instant = Instant.from(dateTime);
      cldt = localInstant(instant, zoneId);
    } catch (DateTimeException ex1) {
      cldt = ensureChronoLocalDateTime(localDateTime(dateTime));
    }
    return ChronoZonedDateTimeImpl.ofBest(cldt, zoneId, null);
  } catch (DateTimeException ex) {
    throw new DateTimeException("Unable to convert DateTimeAccessor to ZonedDateTime: " + dateTime.getClass(), ex);
  }
}
 
Example 5
Project: git-rekt   File: BrowseRoomsScreenController.java   Source Code and License Vote up 6 votes
/**
 * Searches the database for rooms that are available in the given date range.
 */
@FXML
private void onFindAvailableRoomsButtonClicked() {
    // The new date api is great. Converting back and forth, not so much.
    LocalDate checkInDateTemp = checkInDatePicker.getValue();
    LocalDate checkOutDateTemp = checkOutDatePicker.getValue();
    Instant temp1 = Instant.from(checkInDateTemp.atStartOfDay(ZoneId.systemDefault()));
    Instant temp2 = Instant.from(checkOutDateTemp.atStartOfDay(ZoneId.systemDefault()));
    Date checkInDate = Date.from(temp1);
    Date checkOutDate = Date.from(temp2);

    // Clear any existing results
    roomSearchResults.clear();
    selectedRooms.clear();

    // Get the new results
    BookingService bookingService = new BookingService();
    roomSearchResults.addAll(bookingService.getRoomTypesAvailable(checkInDate, checkOutDate));
}
 
Example 6
Project: fernet-java8   File: FernetTest.java   Source Code and License Vote up 5 votes
@Test
public final void expiredTtl() {
    // given
    final Token token = Token.fromString(
            "gAAAAAAdwJ6xAAECAwQFBgcICQoLDA0OD3HkMATM5lFqGaerZ-fWPAl1-szkFVzXTuGb4hR8AKtwcaX1YdykRtfsH-p1YsUD2Q==");
    final Key key = new Key("cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=");
    now = Instant.from(formatter.parse("1985-10-26T01:21:31-07:00"));

    // when
    thrown.expect(TokenValidationException.class);
    thrown.reportMissingExceptionWithMessage("Token should be expired: " + token);
    token.validateAndDecrypt(key, validator);

    // then (nothing)
}
 
Example 7
Project: Pet-Supply-Store   File: TrainingSynchronizer.java   Source Code and License Vote up 5 votes
private static long toMillis(String date) {
	TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(date);
	LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
	ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
	Instant instant = Instant.from(zonedDateTime);
	return instant.toEpochMilli();
}
 
Example 8
Project: carnotzet   File: DockerRegistry.java   Source Code and License Vote up 5 votes
private static Instant getLocalImageTimestamp(String imageName) {
	// Use docker inspect
	try {
		String isoDatetime = DefaultCommandRunner.INSTANCE.runCommandAndCaptureOutput("docker", "inspect", "-f", "{{.Created}}", imageName);
		return Instant.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(isoDatetime));
	}
	catch (RuntimeException e) {
		log.debug("Could not determine timestamp of local image [" + imageName + "], assuming it doesn't exist on the local docker host", e);
		return null;
	}
}
 
Example 9
Project: hippo   File: ExpectedTestDataProvider.java   Source Code and License Vote up 4 votes
private static Instant asInstant(final String timestamp) {
    return Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse(timestamp));
}
 
Example 10
Project: OpenJSharp   File: Chronology.java   Source Code and License Vote up 4 votes
/**
 * 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 11
Project: jdk8u-jdk   File: Chronology.java   Source Code and License Vote up 4 votes
/**
 * 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
Project: openjdk-jdk10   File: Chronology.java   Source Code and License Vote up 4 votes
/**
 * 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);
    }
}