Example usage for java.time ZonedDateTime ofInstant

List of usage examples for java.time ZonedDateTime ofInstant

Introduction

In this page you can find the example usage for java.time ZonedDateTime ofInstant.

Prototype

public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) 

Source Link

Document

Obtains an instance of ZonedDateTime from an Instant .

Usage

From source file:org.silverpeas.core.webapi.reminder.ReminderResource.java

/**
 * Gets the identifier list of possible of durations.
 * <p>/*from   www  .  ja  va 2  s  .com*/
 * An identifier of a duration is the concatenation about the duration value and the duration
 * unit ({@link TimeUnit}).<br>
 * {@code 15MINUTE} for example.
 * </p>
 * @return a filled list if any, or an empty one if no trigger can be scheduled.
 * @see WebProcess#execute()
 */
@Path("possibledurations/{property}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("ConstantConditions")
public List<String> getPossibleDurations(@PathParam("property") final String contributionProperty) {
    if (getSessionVolatileResourceCacheService().contains(localId, componentInstanceId)) {
        return getPossibleReminders().map(DURATION_IDS).collect(Collectors.toList());
    }
    final ContributionModel model = getContribution().getModel();
    final ZoneId userZoneId = getUserPreferences().getZoneId();
    final ZoneId platformZoneId = ZoneId.systemDefault();
    final Mutable<Boolean> lastMatchOk = Mutable.of(true);
    return getPossibleReminders().filter(r -> {
        if (lastMatchOk.is(false)) {
            return false;
        }
        final ZonedDateTime from = ZonedDateTime.now(userZoneId).plus(r.getLeft(), r.getRight().toChronoUnit());
        final ZonedDateTime dateReference = model.filterByType(contributionProperty, from)
                .matchFirst(Date.class::isAssignableFrom,
                        d -> ZonedDateTime.ofInstant(((Date) d).toInstant(), platformZoneId))
                .matchFirst(OffsetDateTime.class::equals,
                        d -> ((OffsetDateTime) d).atZoneSameInstant(platformZoneId))
                .matchFirst(LocalDate.class::equals,
                        d -> ((LocalDate) d).atStartOfDay(userZoneId).withZoneSameInstant(platformZoneId))
                .matchFirst(LocalDateTime.class::equals, d -> ((LocalDateTime) d).atZone(platformZoneId))
                .matchFirst(ZonedDateTime.class::equals,
                        d -> ((ZonedDateTime) d).withZoneSameInstant(platformZoneId))
                .result().orElse(null);
        lastMatchOk.set(dateReference != null);
        return lastMatchOk.get();
    }).map(DURATION_IDS).collect(Collectors.toList());
}

From source file:eu.hansolo.tilesfx.tools.Location.java

public ZonedDateTime getZonedDateTime(final ZoneId ZONE_ID) {
    return ZonedDateTime.ofInstant(timestamp, ZONE_ID);
}

From source file:io.stallion.utils.GeneralUtils.java

@Deprecated
public static String formatLocalDateFromJDate(Date date, String formatPattern) {
    if (date == null) {
        return "";
    }/*from  w  ww .  j a v a  2s .c om*/
    return formatLocalDateFromZonedDate(ZonedDateTime.ofInstant(date.toInstant(), UTC), formatPattern);
}

From source file:io.stallion.utils.GeneralUtils.java

@Deprecated
public static String formatLocalDateFromLong(long epochMillis, String formatPattern) {
    if (epochMillis == 0L) {
        return "";
    }/*from w w  w  .  j  a  va  2  s.  co  m*/
    return formatLocalDateFromZonedDate(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), UTC),
            formatPattern);
}

From source file:org.dhatim.fastexcel.Correctness.java

@Test
public void singleWorksheet() throws Exception {
    String sheetName = "Worksheet 1";
    String stringValue = "Sample text with chars to escape : < > & \\ \" ' ~        ";
    Date dateValue = new Date();
    LocalDateTime localDateTimeValue = LocalDateTime.now();
    ZoneId timezone = ZoneId.of("Australia/Sydney");
    ZonedDateTime zonedDateValue = ZonedDateTime.ofInstant(dateValue.toInstant(), timezone);
    double doubleValue = 1.234;
    int intValue = 2_016;
    long longValue = 2_016_000_000_000L;
    BigDecimal bigDecimalValue = BigDecimal.TEN;
    byte[] data = writeWorkbook(wb -> {
        Worksheet ws = wb.newWorksheet(sheetName);
        int i = 1;
        ws.value(i, i++, stringValue);//from  www .jav a2s  . c o  m
        ws.value(i, i++, dateValue);
        ws.value(i, i++, localDateTimeValue);
        ws.value(i, i++, zonedDateValue);
        ws.value(i, i++, doubleValue);
        ws.value(i, i++, intValue);
        ws.value(i, i++, longValue);
        ws.value(i, i++, bigDecimalValue);
        try {
            ws.finish();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    });

    // Check generated workbook with Apache POI
    XSSFWorkbook xwb = new XSSFWorkbook(new ByteArrayInputStream(data));
    assertThat(xwb.getActiveSheetIndex()).isEqualTo(0);
    assertThat(xwb.getNumberOfSheets()).isEqualTo(1);
    XSSFSheet xws = xwb.getSheet(sheetName);
    @SuppressWarnings("unchecked")
    Comparable<XSSFRow> row = (Comparable) xws.getRow(0);
    assertThat(row).isNull();
    int i = 1;
    assertThat(xws.getRow(i).getCell(i++).getStringCellValue()).isEqualTo(stringValue);
    assertThat(xws.getRow(i).getCell(i++).getDateCellValue()).isEqualTo(dateValue);
    // Check zoned timestamps have the same textual representation as the Dates extracted from the workbook
    // (Excel date serial numbers do not carry timezone information)
    assertThat(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ZonedDateTime
            .ofInstant(xws.getRow(i).getCell(i++).getDateCellValue().toInstant(), ZoneId.systemDefault())))
                    .isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTimeValue));
    assertThat(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(ZonedDateTime
            .ofInstant(xws.getRow(i).getCell(i++).getDateCellValue().toInstant(), ZoneId.systemDefault())))
                    .isEqualTo(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(zonedDateValue));
    assertThat(xws.getRow(i).getCell(i++).getNumericCellValue()).isEqualTo(doubleValue);
    assertThat(xws.getRow(i).getCell(i++).getNumericCellValue()).isEqualTo(intValue);
    assertThat(xws.getRow(i).getCell(i++).getNumericCellValue()).isEqualTo(longValue);
    assertThat(new BigDecimal(xws.getRow(i).getCell(i++).getRawValue())).isEqualTo(bigDecimalValue);
}

From source file:org.codice.ddf.security.idp.client.IdpMetadataTest.java

/**
 * Return a modified version of the (XML) input. The cache duration and valid-until time are
 * modified to match the respective input parameters. If null is passed for the cache duration,
 * the value of the cache duration already in the XML is used. Because of how the substitution
 * works, this method can only be called only once per test. Otherwise, it will create multiple
 * "validUntil" XML attributes./*w w  w. j  a v  a 2 s .  c om*/
 *
 * @param validUntil the validUntil instant
 * @param xml the SAML entity description document
 * @return SAML entity description document with a validUntil date
 */
private String setValidUntil(Instant validUntil, String iso8601Duration, String xml) {
    Pattern pattern = Pattern.compile("cacheDuration=\"(\\w*)\"");
    Matcher matcher = pattern.matcher(xml);
    assertThat("Cannot setup test data - precondition not met", matcher.find(), is(true));
    assertThat("Cannot setup test data - precondition not met", matcher.groupCount(), is(1));
    String duration = iso8601Duration == null ? matcher.group(1) : iso8601Duration;

    DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
    ZonedDateTime temporalAccessor = ZonedDateTime.ofInstant(validUntil, ZoneId.systemDefault());
    String isoTimestamp = formatter.format(temporalAccessor);
    return xml.replaceFirst(CACHE_DURATION_REGEX,
            String.format("cacheDuration=\"%s\" validUntil=\"%s\"", duration, isoTimestamp));
}

From source file:dk.dma.ais.packet.AisPacketCSVOutputSink.java

private void addTimestampPretty(List fields, AisPacket packet) {
    final long timestamp = packet.getBestTimestamp();
    if (timestamp >= 0)
        fields.add(DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss")
                .format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("UTC"))));
    else/*  ww w  . j a v  a2 s  .c  om*/
        fields.add("null");
}

From source file:org.eclipse.smarthome.binding.openweathermap.internal.handler.AbstractOpenWeatherMapHandler.java

protected State getDateTimeTypeState(@Nullable Integer value) {
    return (value == null) ? UnDefType.UNDEF
            : new DateTimeType(
                    ZonedDateTime.ofInstant(Instant.ofEpochSecond(value.longValue()), ZoneId.systemDefault()));
}

From source file:io.stallion.utils.GeneralUtils.java

@Deprecated
public static String formatLocalDateFromLong(Long epochMillis, String formatPattern) {
    if (epochMillis == 0L || epochMillis == null) {
        return "";
    }//w  ww  . j  a v  a2  s  .  c o m
    return formatLocalDateFromZonedDate(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), UTC),
            formatPattern);
}

From source file:org.silverpeas.core.date.DateTime.java

/**
 * Converts this datetime to a {@link ZonedDateTime} instance. The time part as well its
 * timezone is kept./*from  w  w w. ja v  a 2  s .c o  m*/
 * @return a zoned datetime representation of this datetime.
 */
public ZonedDateTime toZoneDateTime() {
    return ZonedDateTime.ofInstant(toInstant(), timeZone.toZoneId());
}