Example usage for java.time ZonedDateTime toInstant

List of usage examples for java.time ZonedDateTime toInstant

Introduction

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

Prototype

default Instant toInstant() 

Source Link

Document

Converts this date-time to an Instant .

Usage

From source file:com.example.app.support.AppUtil.java

/**
 * Convert the given ZonedDateTime to a UTC date for persistence
 *
 * @param dt the ZonedDateTime to convert to UTC
 *
 * @return a Date object that represents the same instant as the ZonedDateTime, but at UTC.
 *//*from w w w  . j  a v  a 2 s  .  co  m*/
@Nullable
public static Date convertForPersistence(@Nullable ZonedDateTime dt) {
    if (dt == null)
        return null;
    ZonedDateTime atUtc = dt.withZoneSameInstant(ZoneOffset.UTC);
    return new Date(atUtc.toInstant().toEpochMilli());
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Convert the given ZonedDateTime to a UTC date for persistence
 *
 * @param dt the ZonedDateTime to convert to UTC
 *
 * @return a Date object that represents the same instant as the ZonedDateTime, but at UTC.
 *//*from  w  w  w .  jav  a  2 s  . c  o  m*/
@Nullable
@Contract(value = "!null->!null;null->null", pure = true)
public static Date convertForPersistence(@Nullable ZonedDateTime dt) {
    if (dt == null)
        return null;
    ZonedDateTime atUtc = dt.withZoneSameInstant(ZoneOffset.UTC);
    return new Date(atUtc.toInstant().toEpochMilli());
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Convert the given ZonedDateTime to a Date
 *
 * @param dt the ZonedDateTime//from w  w w .  j  a va2  s . c o  m
 *
 * @return a Date object that represents the same instant as the ZonedDateTime, at the ZonedDateTime's timezone.
 */
@Nullable
public static Date toDate(@Nullable ZonedDateTime dt) {
    if (dt == null)
        return null;
    return new Date(dt.toInstant().toEpochMilli());
}

From source file:ru.anr.base.BaseParent.java

/**
 * We have to use old Date object, because Hibernate/JPA does not support
 * Java 8 dates (see https://java.net/jira/browse/JPA_SPEC-63 or
 * https://hibernate.atlassian.net/browse/HHH-8844).
 * //from   ww  w .  j  a va2  s  .  c  o  m
 * @param dateTime
 *            Date time in Java 8 format
 * @return Old Date object
 */
public static Date date(ZonedDateTime dateTime) {

    return Date.from(dateTime.toInstant());
}

From source file:com.example.jpa.UserEntity.java

@PrePersist
public void setCreatedAt() {
    final ZonedDateTime now = ZonedDateTime.now();
    final Instant instant = now.toInstant();
    this.createdAt = Date.from(instant);
}

From source file:fi.helsinki.opintoni.service.TimeService.java

public LocalDateTime endOfDayHelsinki(LocalDateTime fromLocalDateTime) {
    ZonedDateTime zonedDateTime = fromLocalDateTime.atZone(HELSINKI_ZONE_ID).withHour(23).withMinute(59)
            .withSecond(59);/*from w  w  w.  j a v a  2 s .c o  m*/
    return LocalDateTime.ofInstant(zonedDateTime.toInstant(), ZoneId.of("UTC"));
}

From source file:com.inversoft.json.ZonedDateTimeSerializer.java

@Override
public void serialize(ZonedDateTime value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    if (value == null) {
        jgen.writeNull();//  w ww. jav a 2 s  . c o m
    } else {
        jgen.writeNumber(value.toInstant().toEpochMilli());
    }
}

From source file:de.loercher.localpress.integration.GeoAndRatingITest.java

@Test
public void testAddArticle() {
    String nowString = "2015-10-12T08:00+02:00[Europe/Berlin]";
    ZonedDateTime now = new DateTimeConverter().fromString(nowString);

    AddArticleEntityDTO geoDTO = new AddArticleEntityDTO(nowString);
    Instant instant = now.toInstant();
    Instant fir = Instant.now();

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("UserID", "ulf");

    HttpEntity<AddArticleEntityDTO> request = new HttpEntity<>(geoDTO, headers);

    RestTemplate template = new RestTemplate();
    ResponseEntity<Map> result = template.postForEntity("http://52.29.77.191:8080/localpress/feedback", request,
            Map.class);

    Instant afterRating = Instant.now();

    GeoBaseEntity.EntityBuilder builder = new GeoBaseEntity.EntityBuilder();
    GeoBaseEntity entity = builder.author("ulf")
            .coordinates(Arrays.asList(new Coordinate[] { new Coordinate(50.1, 8.4) }))
            .timestamp(now.toEpochSecond()).content("abc.de").title("mein titel").user("ulf").build();

    HttpEntity<GeoBaseEntity> second = new HttpEntity<>(entity, headers);
    System.out.println(result.getBody().get("articleID"));

    template.put("http://euve33985.vserver.de:8080/geo/" + result.getBody().get("articleID"), second);

    Instant afterGeoPut = Instant.now();

    result = template.getForEntity("http://euve33985.vserver.de:8080/geo/" + result.getBody().get("articleID"),
            Map.class);

    Instant afterGeoGet = Instant.now();

    assertEquals("User ID has changed over time!", "ulf", result.getBody().get("user"));
    assertEquals("Content URL has changed over time!", "abc.de", result.getBody().get("content"));

    DateTimeConverter conv = new DateTimeConverter();

    Duration first = Duration.between(fir, afterRating);
    Duration sec = Duration.between(afterRating, afterGeoPut);
    Duration third = Duration.between(afterGeoPut, afterGeoGet);

    System.out.println("Begin: " + conv.toString(now));
    System.out.println("Time until POST to rating: " + new Double(first.toMillis()) / 1000);
    System.out.println("Time until PUT to geo: " + new Double(sec.toMillis()) / 1000);
    System.out.println("Time until GET to geo: " + new Double(third.toMillis()) / 1000);
}

From source file:it.tidalwave.northernwind.frontend.media.impl.DefaultMetadataCacheTest.java

/*******************************************************************************************************************
 *
 ******************************************************************************************************************/
private void setTime(final @Nonnull ZonedDateTime dateTime) {
    mockClock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
    log.info("==== Time set to           {}", ZonedDateTime.now(mockClock));
}

From source file:net.ljcomputing.ecsr.security.service.impl.JwtTokenServiceImpl.java

/**
 * The date now./*from   w  w  w . j  a  v  a  2  s.c o m*/
 *
 * @return the date
 */
private Date now() {
    final ZonedDateTime zdt = ldtNow.atZone(ZONE_ID); // NOPMD
    final Instant instant = zdt.toInstant(); // NOPMD
    return Date.from(instant); // NOPMD
}