Example usage for java.time OffsetDateTime plusSeconds

List of usage examples for java.time OffsetDateTime plusSeconds

Introduction

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

Prototype

public OffsetDateTime plusSeconds(long seconds) 

Source Link

Document

Returns a copy of this OffsetDateTime with the specified number of seconds added.

Usage

From source file:Main.java

public static void main(String[] args) {
    OffsetDateTime o = OffsetDateTime.now();
    OffsetDateTime d = o.plusSeconds(20);
    System.out.println(d);/*  w  w  w  .  java 2 s .co m*/
}

From source file:org.openmhealth.shim.misfit.mapper.MisfitSleepDurationDataPointMapper.java

@Override
public Optional<DataPoint<SleepDuration>> asDataPoint(JsonNode sleepNode) {

    // The sleep details array contains segments corresponding to whether the user was awake, sleeping lightly,
    // or sleeping restfully for the duration of that segment. To discount the awake segments, we have to deduct
    // their duration from the total sleep duration.
    JsonNode sleepDetailsNode = asRequiredNode(sleepNode, "sleepDetails");

    long awakeDurationInSec = 0;

    OffsetDateTime previousSegmentStartDateTime = null;
    Long previousSegmentType = null;

    for (JsonNode sleepDetailSegmentNode : sleepDetailsNode) {

        OffsetDateTime startDateTime = asRequiredOffsetDateTime(sleepDetailSegmentNode, "datetime");
        Long value = asRequiredLong(sleepDetailSegmentNode, "value");

        // if the user was awake, add it to the awake tally
        if (previousSegmentType != null && previousSegmentType == AWAKE_SEGMENT_TYPE) {
            awakeDurationInSec += Duration.between(previousSegmentStartDateTime, startDateTime).getSeconds();
        }/*w  ww .j  a va2s .c o  m*/

        previousSegmentStartDateTime = startDateTime;
        previousSegmentType = value;
    }

    // checking if the segment array is empty this way avoids compiler confusion later
    if (previousSegmentType == null) {
        throw new JsonNodeMappingException(
                format("The Misfit sleep node '%s' has no sleep details.", sleepNode));
    }

    // to calculate the duration of last segment, first determine the overall end time
    OffsetDateTime startDateTime = asRequiredOffsetDateTime(sleepNode, "startTime");
    Long totalDurationInSec = asRequiredLong(sleepNode, "duration");
    OffsetDateTime endDateTime = startDateTime.plusSeconds(totalDurationInSec);

    if (previousSegmentType == AWAKE_SEGMENT_TYPE) {
        awakeDurationInSec += Duration.between(previousSegmentStartDateTime, endDateTime).getSeconds();
    }

    Long sleepDurationInSec = totalDurationInSec - awakeDurationInSec;

    if (sleepDurationInSec == 0) {
        return Optional.empty();
    }

    SleepDuration measure = new SleepDuration.Builder(new DurationUnitValue(SECOND, sleepDurationInSec))
            .setEffectiveTimeFrame(ofStartDateTimeAndEndDateTime(startDateTime, endDateTime)).build();

    String externalId = asOptionalString(sleepNode, "id").orElse(null);
    Boolean sensed = asOptionalBoolean(sleepNode, "autoDetected").orElse(null);

    return Optional.of(newDataPoint(measure, RESOURCE_API_SOURCE_NAME, externalId, sensed));
}