Example usage for java.time ZonedDateTime of

List of usage examples for java.time ZonedDateTime of

Introduction

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

Prototype

public static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone) 

Source Link

Document

Obtains an instance of ZonedDateTime from a local date-time.

Usage

From source file:Main.java

public static void main(String[] args) {
    ZoneId usChicago = ZoneId.of("America/Chicago");

    // 2014-03-09T02:30 did not exist in America/Chicago time zone
    LocalDateTime ldt = LocalDateTime.of(2014, Month.MARCH, 9, 2, 30);
    ZonedDateTime zdt = ZonedDateTime.of(ldt, usChicago);
    System.out.println(zdt);//from   ww  w. j a  v a 2s .c  om

    // 2013-10-03T01:30 existed twice in America/Chicago time zone
    LocalDateTime ldt2 = LocalDateTime.of(2013, Month.NOVEMBER, 3, 1, 30);
    ZonedDateTime zdt2 = ZonedDateTime.of(ldt2, usChicago);

    System.out.println(zdt2.withEarlierOffsetAtOverlap());
    System.out.println(zdt2.withLaterOffsetAtOverlap());

}

From source file:Main.java

public static void main(String[] args) {
    ZonedDateTime zdt1 = ZonedDateTime.now();
    System.out.println("Current zoned  datetime:" + zdt1);

    LocalDateTime ldt = LocalDateTime.of(2012, Month.MARCH, 11, 7, 30);

    ZoneId usCentralZone = ZoneId.of("America/Chicago");
    ZonedDateTime zdt2 = ZonedDateTime.of(ldt, usCentralZone);
    System.out.println(zdt2);/*from   w  ww. j  a  v a2 s  .  co m*/
}

From source file:Main.java

public static void main(String[] args) {
    ZoneId berlin = ZoneId.of("Europe/Berlin");
    ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
    // 2014-02-20 12:00
    LocalDateTime dateTime = LocalDateTime.of(2014, 02, 20, 12, 0);

    // 2014-02-20 12:00, Europe/Berlin (+01:00)
    ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, berlin);

    // 2014-02-20 03:00, America/Los_Angeles (-08:00)
    ZonedDateTime losAngelesDateTime = berlinDateTime.withZoneSameInstant(losAngeles);

    System.out.println(losAngelesDateTime);
}

From source file:Main.java

public static void main(String[] args) {
    ZoneId berlin = ZoneId.of("Europe/Berlin");
    ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
    // 2014-02-20 12:00
    LocalDateTime dateTime = LocalDateTime.of(2014, 02, 20, 12, 0);

    // 2014-02-20 12:00, Europe/Berlin (+01:00)
    ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, berlin);

    // 2014-02-20 03:00, America/Los_Angeles (-08:00)
    ZonedDateTime losAngelesDateTime = berlinDateTime.withZoneSameInstant(losAngeles);

    int offsetInSeconds = losAngelesDateTime.getOffset().getTotalSeconds(); // -28800

    System.out.println(offsetInSeconds);
}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");
    LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);

    // Leaving from San Francisco on July 20, 2013, at 7:30 p.m.

    ZoneId leavingZone = ZoneId.of("America/Los_Angeles");
    ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);

    String out1 = departure.format(format);
    System.out.printf("LEAVING:  %s (%s)%n", out1, leavingZone);

    // Flight is 10 hours and 50 minutes, or 650 minutes
    ZoneId arrivingZone = ZoneId.of("Asia/Tokyo");
    ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone).plusMinutes(650);

    String out2 = arrival.format(format);
    System.out.printf("ARRIVING: %s (%s)%n", out2, arrivingZone);

}

From source file:Main.java

public static void main(String[] args) {
    LocalTime now = LocalTime.now();
    LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));
    System.out.println(String.format("now is %s and in LA is %s", now, currentTimeInLosAngeles));

    ZoneId leavingZone = ZoneId.of("Asia/Tel_Aviv");
    ZoneId arrivingZone = ZoneId.of("America/New_York");

    LocalDateTime leaving = LocalDateTime.of(2014, Month.JULY, 16, 23, 00);
    ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);

    ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone).plusHours(11).plusMinutes(51);

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-d  HH:mm");
    System.out.println(String.format("Departure: %s", departure.format(format)));
    System.out.println(String.format("Arrival: %s", arrival.format(format)));
}

From source file:Main.java

private static long getMillis(String strDate, DateTimeFormatter formatter, ZoneId zone) {
    LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone);
    Instant instant = zonedDateTime.toInstant();
    long milli = instant.toEpochMilli();
    return milli;
}

From source file:Main.java

/**
 * Checks if the type of the given date. Possible return values are standard
 * time, the date when to switch to daylight saving time (in Europe the last
 * Sunday in March), daylight saving time or the date when to switch back to
 * standard time (in Europe the last Sunday in October).
 * //from w w w  . jav  a 2 s .c  o  m
 * @return DayType
 * @param cal
 *          Date to check, cannot be null
 */
public static DayType getDSTType(LocalDate cal) {
    DayType status = DayType.DAYLIGHT_SAVING_TIME;
    LocalDateTime testDate = cal.atStartOfDay();
    ZonedDateTime zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault());
    // Find type of day
    if (zdt.getZone().getRules().isDaylightSavings(testDate.toInstant(zdt.getOffset())))
        status = DayType.DAYLIGHT_SAVING_TIME;
    else
        status = DayType.STANDARD_TIME;
    // Check the day after
    testDate = testDate.plusDays(1);
    zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault());
    // Find type of day after
    if (zdt.getZone().getRules().isDaylightSavings(testDate.toInstant(zdt.getOffset()))) {
        if (status != DayType.DAYLIGHT_SAVING_TIME)
            status = DayType.TO_DAYLIGHT_SAVING_TIME;
    } else {
        if (status == DayType.DAYLIGHT_SAVING_TIME)
            status = DayType.TO_STANDARD_TIME;
    }
    return status;
}

From source file:defaultmethods.TimeClient.java

default ZonedDateTime getZonedDateTime(String zoneString) {
    return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
}

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 ww.  java 2  s.  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);
}