Example usage for java.time OffsetDateTime of

List of usage examples for java.time OffsetDateTime of

Introduction

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

Prototype

public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) 

Source Link

Document

Obtains an instance of OffsetDateTime from a date-time and offset.

Usage

From source file:Main.java

public static void main(String[] args) {
    OffsetDateTime o = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.UTC);

    System.out.println(o);
}

From source file:Main.java

public static void main(String[] argv) {
    ZoneId INDIA = ZoneId.of("Asia/Kolkata");

    LocalDateTime datetime = LocalDateTime.now();
    ZoneOffset offset = ZoneOffset.of("+05:30");
    OffsetDateTime date = OffsetDateTime.of(datetime, offset);

    System.out.println("OffsetDateTime.now() = " + OffsetDateTime.now());
    System.out.println("OffsetDateTime.now(TimeZone.INDIA) = " + OffsetDateTime.now(INDIA));
    System.out.println("Date and Time: " + datetime);
    System.out.println("Date and Time with timezone offset: " + date);
}

From source file:Main.java

public static void main(String[] args) {
    // using offsets
    LocalDateTime date = LocalDateTime.of(2013, Month.JULY, 20, 3, 30);
    ZoneOffset offset = ZoneOffset.of("+05:00");

    // 2013-07-20 22:30 +05:00
    OffsetDateTime plusFive = OffsetDateTime.of(date, offset);
    System.out.println(plusFive);

    // 2013-07-19 20:30 -02:00
    OffsetDateTime minusTwo = plusFive.withOffsetSameInstant(ZoneOffset.ofHours(-2));

    System.out.println(minusTwo);
}

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

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * StepCount} data point./*  w w w .j  av  a2s . c o  m*/
 *
 * @param node 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}
 */
@Override
Optional<DataPoint<StepCount>> asDataPoint(JsonNode node) {

    long stepValue = asRequiredLong(node, "steps");
    StepCount.Builder stepCountBuilder = new StepCount.Builder(stepValue);
    Optional<String> dateString = asOptionalString(node, "date");
    Optional<String> timeZoneFullName = asOptionalString(node, "timezone");
    // We assume that timezone is the same for both the startdate and enddate timestamps, even though Withings only
    // provides the enddate timezone as the "timezone" property.
    // TODO: Revisit once Withings can provide start_timezone and end_timezone
    if (dateString.isPresent() && timeZoneFullName.isPresent()) {
        LocalDateTime localStartDateTime = LocalDate.parse(dateString.get()).atStartOfDay();
        ZoneId zoneId = ZoneId.of(timeZoneFullName.get());
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localStartDateTime, zoneId);
        ZoneOffset offset = zonedDateTime.getOffset();
        OffsetDateTime offsetStartDateTime = OffsetDateTime.of(localStartDateTime, offset);
        LocalDateTime localEndDateTime = LocalDate.parse(dateString.get()).atStartOfDay().plusDays(1);
        OffsetDateTime offsetEndDateTime = OffsetDateTime.of(localEndDateTime, offset);
        stepCountBuilder.setEffectiveTimeFrame(
                TimeInterval.ofStartDateTimeAndEndDateTime(offsetStartDateTime, offsetEndDateTime));
    }

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

    StepCount stepCount = stepCountBuilder.build();
    DataPoint<StepCount> stepCountDataPoint = newDataPoint(stepCount, null, true, null);

    return Optional.of(stepCountDataPoint);
}

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

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * CaloriesBurned} data point./*from w ww  . j a  v  a 2s . co  m*/
 * <p>
 * <p>Note: the start datetime and end datetime values for the mapped {@link CaloriesBurned} {@link DataPoint}
 * assume that
 * the start timezone and end time zone are the same, both equal to the "timezone" property in the Withings
 * response
 * datapoints. However, according to Withings, the property value they provide is specifically the end datetime
 * timezone.</p>
 *
 * @param node activity node from the array "activites" contained in the "body" of the endpoint response
 * @return a {@link DataPoint} object containing a {@link CaloriesBurned} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
@Override
Optional<DataPoint<CaloriesBurned>> asDataPoint(JsonNode node) {

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

    Optional<String> dateString = asOptionalString(node, "date");
    Optional<String> timeZoneFullName = asOptionalString(node, "timezone");
    // We assume that timezone is the same for both the startdate and enddate timestamps, even though Withings only
    // provides the enddate timezone as the "timezone" property.
    // TODO: Revisit once Withings can provide start_timezone and end_timezone
    if (dateString.isPresent() && timeZoneFullName.isPresent()) {
        LocalDateTime localStartDateTime = LocalDate.parse(dateString.get()).atStartOfDay();
        ZoneId zoneId = ZoneId.of(timeZoneFullName.get());
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localStartDateTime, zoneId);
        ZoneOffset offset = zonedDateTime.getOffset();
        OffsetDateTime offsetStartDateTime = OffsetDateTime.of(localStartDateTime, offset);
        LocalDateTime localEndDateTime = LocalDate.parse(dateString.get()).atStartOfDay().plusDays(1);

        OffsetDateTime offsetEndDateTime = OffsetDateTime.of(localEndDateTime, offset);
        caloriesBurnedBuilder.setEffectiveTimeFrame(
                TimeInterval.ofStartDateTimeAndEndDateTime(offsetStartDateTime, offsetEndDateTime));
    }

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

    CaloriesBurned caloriesBurned = caloriesBurnedBuilder.build();
    DataPoint<CaloriesBurned> caloriesBurnedDataPoint = newDataPoint(caloriesBurned, null, true, null);

    return Optional.of(caloriesBurnedDataPoint);

}

From source file:org.openmhealth.shim.fitbit.mapper.FitbitDataPointMapper.java

/**
 * TODO rewrite this, the names don't make sense
 * @param node a JSON node containing <code>date</code> and <code>time</code> properties
 * @return the equivalent OffsetDateTime
 *//*from   www .j a  va2s .  c o m*/
protected Optional<OffsetDateTime> combineDateTimeAndTimezone(JsonNode node) {

    Optional<LocalDateTime> dateTime = asOptionalLocalDateTime(node, "date", "time");
    Optional<OffsetDateTime> offsetDateTime = null;

    if (dateTime.isPresent()) {
        // FIXME fix the time zone offset to use the correct offset for the data point once it is fixed by Fitbit
        offsetDateTime = Optional.of(OffsetDateTime.of(dateTime.get(), ZoneOffset.UTC));
    }

    return offsetDateTime;
}

From source file:org.openmhealth.shim.fitbit.mapper.FitbitDataPointMapper.java

/**
 * Transforms a {@link LocalDateTime} object into an {@link OffsetDateTime} object with a UTC time zone
 *
 * @param dateTime local date and time for the Fitbit response JSON node
 * @return the date and time based on the input dateTime parameter
 *//*  w  w  w .ja v a  2s . c om*/
protected OffsetDateTime combineDateTimeAndTimezone(LocalDateTime dateTime) {

    // FIXME fix the time zone offset to use the appropriate offset for the data point once it is fixed by Fitbit
    return OffsetDateTime.of(dateTime, ZoneOffset.UTC);
}

From source file:net.dv8tion.jda.core.EmbedBuilder.java

/**
 * Sets the Timestamp of the embed./*from   www  .j a  va 2s .  com*/
 *
 * <p><b><a href="http://i.imgur.com/YP4NiER.png">Example</a></b>
 *
 * <p><b>Hint:</b> You can get the current time using {@link java.time.Instant#now() Instant.now()} or convert time from a
 * millisecond representation by using {@link java.time.Instant#ofEpochMilli(long) Instant.ofEpochMilli(long)};
 *
 * @param  temporal
 *         the temporal accessor of the timestamp
 *
 * @return the builder after the timestamp has been set
 */
public EmbedBuilder setTimestamp(TemporalAccessor temporal) {
    if (temporal == null) {
        this.timestamp = null;
    } else if (temporal instanceof OffsetDateTime) {
        this.timestamp = (OffsetDateTime) temporal;
    } else {
        ZoneOffset offset;
        try {
            offset = ZoneOffset.from(temporal);
        } catch (DateTimeException ignore) {
            offset = ZoneOffset.UTC;
        }
        try {
            LocalDateTime ldt = LocalDateTime.from(temporal);
            this.timestamp = OffsetDateTime.of(ldt, offset);
        } catch (DateTimeException ignore) {
            try {
                Instant instant = Instant.from(temporal);
                this.timestamp = OffsetDateTime.ofInstant(instant, offset);
            } catch (DateTimeException ex) {
                throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + temporal
                        + " of type " + temporal.getClass().getName(), ex);
            }
        }
    }
    return this;
}