Example usage for java.time OffsetDateTime toInstant

List of usage examples for java.time OffsetDateTime toInstant

Introduction

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

Prototype

public Instant toInstant() 

Source Link

Document

Converts this date-time to an Instant .

Usage

From source file:Main.java

public static void main(String[] args) {
    OffsetDateTime o = OffsetDateTime.MAX;
    Instant i = o.toInstant();
    System.out.println(i.getNano());
}

From source file:org.onosproject.drivers.polatis.netconf.PolatisAlarmConsumer.java

private long getTimeRaised(HierarchicalConfiguration cfg) {
    long timeRaised;

    String alarmTime = cfg.getString(ALARM_TIME);
    try {// www . j av a 2  s  .c o m
        OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        timeRaised = date.toInstant().toEpochMilli();
        return timeRaised;
    } catch (DateTimeException e) {
        log.error("Cannot parse exception {} {}", alarmTime, e);
    }
    return System.currentTimeMillis();
}

From source file:org.silverpeas.core.calendar.CalendarEventStubBuilder.java

public CalendarEventStubBuilder withCreationDate(final OffsetDateTime createDate) {
    component.withCreateDate(Date.from(createDate.toInstant()));
    return this;
}

From source file:org.silverpeas.core.calendar.CalendarEventStubBuilder.java

public CalendarEventStubBuilder withLastUpdateDate(final OffsetDateTime lastUpdateDate) {
    component.withLastUpdateDate(Date.from(lastUpdateDate.toInstant()));
    return this;
}

From source file:org.openhab.binding.dwdunwetter.internal.data.DwdWarningsData.java

private Instant getTimestampValue(XMLEventReader eventReader) throws XMLStreamException {
    XMLEvent event = eventReader.nextEvent();
    String dateTimeString = event.asCharacters().getData();
    try {//from   w w w.j a  v  a  2 s.  c om
        OffsetDateTime dateTime = OffsetDateTime.parse(dateTimeString, formatter);
        return dateTime.toInstant();
    } catch (DateTimeParseException e) {
        logger.debug("Exception while parsing a DateTime", e);
        return Instant.MIN;
    }
}

From source file:org.jimsey.projects.turbine.fuel.domain.Entity.java

public Entity(OffsetDateTime date, double close, String symbol, String market, String name, String timestamp) {
    this.timestamp = date;
    this.close = close;
    this.symbol = symbol;
    this.market = market;
    this.name = name;
    try {//from  w  ww .j  a v a2  s.  com
        this.date = date.toInstant().toEpochMilli();
    } catch (Exception e) {
        logger.warn("Could not parse date: {}", date.toString());
    }
    try {
        this.timestamp = OffsetDateTime.parse(timestamp);
    } catch (Exception e) {
        logger.warn("Could not parse timestamp: {}", timestamp);
    }

}

From source file:net.dv8tion.jda.core.requests.ratelimit.BotRateLimiter.java

private void setTimeOffset(Headers headers) {
    //Store as soon as possible to get the most accurate time difference;
    long time = System.currentTimeMillis();
    if (timeOffset == null) {
        //Get the date header provided by Discord.
        //Format:  "date" : "Fri, 16 Sep 2016 05:49:36 GMT"
        String date = headers.get("Date");
        if (date != null) {
            OffsetDateTime tDate = OffsetDateTime.parse(date, DateTimeFormatter.RFC_1123_DATE_TIME);
            long lDate = tDate.toInstant().toEpochMilli(); //We want to work in milliseconds, not seconds
            timeOffset = lDate - time; //Get offset in milliseconds.
        }/*from  w  ww . java2  s .  c o  m*/
    }
}

From source file:org.cryptomator.frontend.webdav.servlet.DavNode.java

@Override
public void setProperty(DavProperty<?> property) throws DavException {
    final String namespacelessPropertyName = property.getName().getName();
    if (Arrays.asList(DAV_CREATIONDATE_PROPNAMES).contains(namespacelessPropertyName)
            && property.getValue() instanceof String) {
        String createDateStr = (String) property.getValue();
        OffsetDateTime creationDate = OffsetDateTime
                .from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(createDateStr));
        this.setCreationTime(creationDate.toInstant());
    } else if (Arrays.asList(DAV_MODIFIEDDATE_PROPNAMES).contains(namespacelessPropertyName)
            && property.getValue() instanceof String) {
        String lastModifiedDateStr = (String) property.getValue();
        OffsetDateTime lastModifiedDate = OffsetDateTime
                .from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(lastModifiedDateStr));
        this.setModificationTime(lastModifiedDate.toInstant());
    }/*from  w w  w  .  j a va2  s .c  o m*/
    properties.add(property);
}

From source file:org.openmhealth.shim.googlefit.GoogleFitShim.java

protected ResponseEntity<ShimDataResponse> getData(OAuth2RestOperations restTemplate,
        ShimDataRequest shimDataRequest) throws ShimException {
    final GoogleFitDataTypes googleFitDataType;
    try {/*  w ww . j a  v  a  2  s .  c om*/
        googleFitDataType = GoogleFitDataTypes.valueOf(shimDataRequest.getDataTypeKey().trim().toUpperCase());
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new ShimException("Null or Invalid data type parameter: " + shimDataRequest.getDataTypeKey()
                + " in shimDataRequest, cannot retrieve data.");
    }

    OffsetDateTime todayInUTC = LocalDate.now().atStartOfDay().atOffset(ZoneOffset.UTC);

    OffsetDateTime startDateInUTC = shimDataRequest.getStartDateTime() == null ? todayInUTC.minusDays(1)
            : shimDataRequest.getStartDateTime();
    long startTimeNanos = (startDateInUTC.toEpochSecond() * 1000000000) + startDateInUTC.toInstant().getNano();

    OffsetDateTime endDateInUTC = shimDataRequest.getEndDateTime() == null ? todayInUTC.plusDays(1)
            : shimDataRequest.getEndDateTime().plusDays(1); // We are inclusive of the last day, so add 1 day to get
    // the end of day on the last day, which captures the
    // entire last day
    long endTimeNanos = (endDateInUTC.toEpochSecond() * 1000000000) + endDateInUTC.toInstant().getNano();

    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(DATA_URL)
            .pathSegment(googleFitDataType.getStreamId(), "datasets", "{startDate}-{endDate}");
    // TODO: Add limits back into the request once Google has fixed the 'limit' query parameter and paging

    URI uriRequest = uriBuilder.buildAndExpand(startTimeNanos, endTimeNanos).encode().toUri();

    ResponseEntity<JsonNode> responseEntity;
    try {
        responseEntity = restTemplate.getForEntity(uriRequest, JsonNode.class);
    } catch (HttpClientErrorException | HttpServerErrorException e) {
        // TODO figure out how to handle this
        logger.error("A request for Google Fit data failed.", e);
        throw e;
    }

    if (shimDataRequest.getNormalize()) {
        GoogleFitDataPointMapper<?> dataPointMapper;
        switch (googleFitDataType) {
        case BODY_WEIGHT:
            dataPointMapper = new GoogleFitBodyWeightDataPointMapper();
            break;
        case BODY_HEIGHT:
            dataPointMapper = new GoogleFitBodyHeightDataPointMapper();
            break;
        case ACTIVITY:
            dataPointMapper = new GoogleFitPhysicalActivityDataPointMapper();
            break;
        case STEP_COUNT:
            dataPointMapper = new GoogleFitStepCountDataPointMapper();
            break;
        case HEART_RATE:
            dataPointMapper = new GoogleFitHeartRateDataPointMapper();
            break;
        case CALORIES_BURNED:
            dataPointMapper = new GoogleFitCaloriesBurnedDataPointMapper();
            break;
        default:
            throw new UnsupportedOperationException();
        }

        return ok().body(ShimDataResponse.result(GoogleFitShim.SHIM_KEY,
                dataPointMapper.asDataPoints(singletonList(responseEntity.getBody()))));
    } else {

        return ok().body(ShimDataResponse.result(GoogleFitShim.SHIM_KEY, responseEntity.getBody()));
    }
}

From source file:org.jimsey.projects.turbine.fuel.domain.TickJson.java

public TickJson(OffsetDateTime date, double open, double high, double low, double close, double volume,
        String symbol, String market, String timestamp) {
    super(DateTime.parse(date.toString(), ISODateTimeFormat.dateTimeParser()), open, high, low, close, volume);
    this.timestamp = date;
    this.symbol = symbol;
    this.market = market;
    try {/*from  w w  w  .  j  av  a2s  .  co m*/
        this.date = date.toInstant().toEpochMilli();
    } catch (Exception e) {
        logger.warn("Could not parse date: {}", date.toString());
    }
    try {
        this.timestamp = OffsetDateTime.parse(timestamp);
    } catch (Exception e) {
        logger.warn("Could not parse timestamp: {}", timestamp);
    }
}