Example usage for java.time OffsetDateTime ofInstant

List of usage examples for java.time OffsetDateTime ofInstant

Introduction

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

Prototype

public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) 

Source Link

Document

Obtains an instance of OffsetDateTime from an Instant and zone ID.

Usage

From source file:Main.java

public static void main(String[] args) {
    OffsetDateTime o = OffsetDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());

    System.out.println(o);//  w  w w  . j av a2s  .c  o m
}

From source file:Main.java

public static void main(String[] args) {
    Instant i1 = Instant.now();

    ZoneId usChicago = ZoneId.of("America/Chicago");
    OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(i1, usChicago);
    System.out.println(offsetDateTime);
}

From source file:com.intuit.wasabi.api.pagination.filters.FilterUtil.java

/**
 * Converts the old {@link Date} to a new {@link OffsetDateTime}, taking the UTC offset into account.
 *
 * @param date the date to convert//from   w w w  . j  a  va2 s  . c o m
 * @return the converted date
 */
public static OffsetDateTime convertDateToOffsetDateTime(Date date) {
    return OffsetDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
}

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

@JsonCreator
public IndicatorJson(@JsonProperty("date") long date, @JsonProperty("close") double close,
        @JsonProperty("indicators") Map<String, Double> indicators, @JsonProperty("symbol") String symbol,
        @JsonProperty("market") String market, @JsonProperty("name") String name,
        @JsonProperty("timestamp") String timestamp) {
    this(OffsetDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault()), close, indicators,
            symbol, market, name, timestamp);
}

From source file:org.openmhealth.shim.withings.mapper.WithingsSleepDurationDataPointMapper.java

/**
 * Maps an individual list node from the array in the Withings sleep summary endpoint response into a {@link
 * SleepDuration} data point.//w  w  w. j a v  a2  s.  c  om
 *
 * @param node activity node from the array "series" contained in the "body" of the endpoint response
 * @return a {@link DataPoint} object containing a {@link SleepDuration} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
@Override
Optional<DataPoint<SleepDuration>> asDataPoint(JsonNode node) {

    Long lightSleepInSeconds = asRequiredLong(node, "data.lightsleepduration");
    Long deepSleepInSeconds = asRequiredLong(node, "data.deepsleepduration");
    Long remSleepInSeconds = asRequiredLong(node, "data.remsleepduration");

    Long totalSleepInSeconds = lightSleepInSeconds + deepSleepInSeconds + remSleepInSeconds;

    SleepDuration.Builder sleepDurationBuilder = new SleepDuration.Builder(
            new DurationUnitValue(DurationUnit.SECOND, totalSleepInSeconds));

    Optional<Long> startDateInEpochSeconds = asOptionalLong(node, "startdate");
    Optional<Long> endDateInEpochSeconds = asOptionalLong(node, "enddate");

    if (startDateInEpochSeconds.isPresent() && endDateInEpochSeconds.isPresent()) {
        OffsetDateTime offsetStartDateTime = OffsetDateTime
                .ofInstant(Instant.ofEpochSecond(startDateInEpochSeconds.get()), of("Z"));
        OffsetDateTime offsetEndDateTime = OffsetDateTime
                .ofInstant(Instant.ofEpochSecond(endDateInEpochSeconds.get()), of("Z"));
        sleepDurationBuilder.setEffectiveTimeFrame(
                TimeInterval.ofStartDateTimeAndEndDateTime(offsetStartDateTime, offsetEndDateTime));
    }

    Optional<Long> externalId = asOptionalLong(node, "id");
    Optional<Long> modelId = asOptionalLong(node, "model");
    String modelName = null;

    if (modelId.isPresent()) {
        modelName = SleepDeviceTypes.valueOf(modelId.get());
    }

    SleepDuration sleepDuration = sleepDurationBuilder.build();
    Optional<Long> wakeupCount = asOptionalLong(node, "data.wakeupcount");
    if (wakeupCount.isPresent()) {
        sleepDuration.setAdditionalProperty("wakeup_count", new Integer(wakeupCount.get().intValue()));
    }

    // These sleep phase values are Withings platform-specific, so we pass them through as additionalProperties to
    // ensure we keep relevant platform specific values. Should be interpreted according to Withings API spec
    sleepDuration.setAdditionalProperty("light_sleep_duration",
            new DurationUnitValue(DurationUnit.SECOND, lightSleepInSeconds));
    sleepDuration.setAdditionalProperty("deep_sleep_duration",
            new DurationUnitValue(DurationUnit.SECOND, deepSleepInSeconds));
    sleepDuration.setAdditionalProperty("rem_sleep_duration",
            new DurationUnitValue(DurationUnit.SECOND, remSleepInSeconds));

    // This is an additional piece of information captured by Withings devices around sleep and should be
    // interpreted according to the Withings API specification. We do not capture durationtowakeup or
    // wakeupduration properties from the Withings API because it is unclear the distinction between them and we
    // aim to avoid creating more ambiguity through passing through these properties
    Optional<Long> timeToSleepValue = asOptionalLong(node, "data.durationtosleep");
    if (timeToSleepValue.isPresent()) {
        sleepDuration.setAdditionalProperty("duration_to_sleep",
                new DurationUnitValue(DurationUnit.SECOND, timeToSleepValue.get()));
    }

    return Optional.of(newDataPoint(sleepDuration, externalId.orElse(null), true, modelName));
}

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

@JsonCreator
public StrategyJson(@JsonProperty("date") long date, @JsonProperty("symbol") String symbol,
        @JsonProperty("market") String market, @JsonProperty("close") double close,
        @JsonProperty("name") String name, @JsonProperty("action") String action,
        @JsonProperty("amount") Integer amount, @JsonProperty("position") Integer position,
        @JsonProperty("cash") Double cash, @JsonProperty("value") Double value,
        @JsonProperty("timestamp") String timestamp) {
    this(OffsetDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault()), market, symbol, close,
            name, action, amount, position, cash, value, timestamp);
}

From source file:org.openmhealth.shim.withings.mapper.WithingsBodyMeasureDataPointMapper.java

@Override
public List<DataPoint<T>> asDataPoints(List<JsonNode> responseNodes) {

    checkNotNull(responseNodes);/*from ww  w.j a v a 2  s.  c om*/
    checkNotNull(responseNodes.size() == 1, "A single response node is allowed per call.");

    JsonNode responseNodeBody = asRequiredNode(responseNodes.get(0), BODY_NODE_PROPERTY);
    List<DataPoint<T>> dataPoints = Lists.newArrayList();

    for (JsonNode measureGroupNode : asRequiredNode(responseNodeBody, "measuregrps")) {

        if (isGoal(measureGroupNode)) {
            continue;
        }

        if (isOwnerAmbiguous(measureGroupNode)) {
            logger.warn(
                    "The following Withings measure group is being ignored because its owner is ambiguous.\n{}",
                    measureGroupNode);
            continue;
        }

        JsonNode measuresNode = asRequiredNode(measureGroupNode, "measures");

        Measure.Builder<T, ?> measureBuilder = newMeasureBuilder(measuresNode).orElse(null);
        if (measureBuilder == null) {
            continue;
        }

        Optional<Long> dateTimeInEpochSeconds = asOptionalLong(measureGroupNode, "date");
        if (dateTimeInEpochSeconds.isPresent()) {

            Instant dateTimeInstant = Instant.ofEpochSecond(dateTimeInEpochSeconds.get());
            measureBuilder.setEffectiveTimeFrame(OffsetDateTime.ofInstant(dateTimeInstant, UTC));
        }

        Optional<String> userComment = asOptionalString(measureGroupNode, "comment");
        if (userComment.isPresent()) {
            measureBuilder.setUserNotes(userComment.get());
        }

        T measure = measureBuilder.build();

        Optional<Long> externalId = asOptionalLong(measureGroupNode, "grpid");

        dataPoints.add(newDataPoint(measure, externalId.orElse(null), isSensed(measureGroupNode), null));
    }

    return dataPoints;
}

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

@JsonCreator
public TickJson(@JsonProperty("date") long date, @JsonProperty("open") double open,
        @JsonProperty("high") double high, @JsonProperty("low") double low, @JsonProperty("close") double close,
        @JsonProperty("volume") double volume, @JsonProperty("symbol") String symbol,
        @JsonProperty("market") String market, @JsonProperty("timestamp") String timestamp) {
    this(OffsetDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault()), open, high, low, close,
            volume, symbol, market, timestamp);
}

From source file:org.openmhealth.shim.withings.mapper.WithingsIntradayStepCountDataPointMapper.java

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * StepCount} data point.//from  ww  w.  jav  a  2 s . c om
 *
 * @param nodeWithSteps activity node from the array "activites" contained in the "body" of the endpoint response
 * @return a {@link DataPoint} object containing a {@link StepCount} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
private Optional<DataPoint<StepCount>> asDataPoint(JsonNode nodeWithSteps,
        Long startDateTimeInUnixEpochSeconds) {
    Long stepCountValue = asRequiredLong(nodeWithSteps, "steps");
    StepCount.Builder stepCountBuilder = new StepCount.Builder(stepCountValue);

    Optional<Long> duration = asOptionalLong(nodeWithSteps, "duration");
    if (duration.isPresent()) {
        OffsetDateTime offsetDateTime = OffsetDateTime
                .ofInstant(Instant.ofEpochSecond(startDateTimeInUnixEpochSeconds), ZoneId.of("Z"));
        stepCountBuilder.setEffectiveTimeFrame(TimeInterval.ofStartDateTimeAndDuration(offsetDateTime,
                new DurationUnitValue(DurationUnit.SECOND, duration.get())));
    }

    Optional<String> userComment = asOptionalString(nodeWithSteps, "comment");
    if (userComment.isPresent()) {
        stepCountBuilder.setUserNotes(userComment.get());
    }

    StepCount stepCount = stepCountBuilder.build();
    return Optional.of(newDataPoint(stepCount, null, true, null));
}

From source file:org.openmhealth.shim.withings.mapper.WithingsIntradayCaloriesBurnedDataPointMapper.java

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * CaloriesBurned} data point./*from ww w  .  jav  a 2 s  . co  m*/
 *
 * @param nodeWithCalorie activity node from the array "activites" contained in the "body" of the endpoint response
 * that has a calories field
 * @return a {@link DataPoint} object containing a {@link CaloriesBurned} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
private Optional<DataPoint<CaloriesBurned>> asDataPoint(JsonNode nodeWithCalorie,
        Long startDateTimeInUnixEpochSeconds) {

    Long caloriesBurnedValue = asRequiredLong(nodeWithCalorie, "calories");
    CaloriesBurned.Builder caloriesBurnedBuilder = new CaloriesBurned.Builder(
            new KcalUnitValue(KcalUnit.KILOCALORIE, caloriesBurnedValue));

    Optional<Long> duration = asOptionalLong(nodeWithCalorie, "duration");
    if (duration.isPresent()) {
        OffsetDateTime offsetDateTime = OffsetDateTime
                .ofInstant(Instant.ofEpochSecond(startDateTimeInUnixEpochSeconds), ZoneId.of("Z"));
        caloriesBurnedBuilder.setEffectiveTimeFrame(TimeInterval.ofStartDateTimeAndDuration(offsetDateTime,
                new DurationUnitValue(DurationUnit.SECOND, duration.get())));
    }

    Optional<String> userComment = asOptionalString(nodeWithCalorie, "comment");
    if (userComment.isPresent()) {
        caloriesBurnedBuilder.setUserNotes(userComment.get());
    }

    CaloriesBurned calorieBurned = caloriesBurnedBuilder.build();
    return Optional.of(newDataPoint(calorieBurned, null, true, null));

}