Example usage for java.time OffsetDateTime withOffsetSameInstant

List of usage examples for java.time OffsetDateTime withOffsetSameInstant

Introduction

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

Prototype

public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) 

Source Link

Document

Returns a copy of this OffsetDateTime with the specified offset ensuring that the result is at the same instant.

Usage

From source file:Main.java

public static void main(String[] args) {
    OffsetDateTime o = OffsetDateTime.now();
    OffsetDateTime l = o.withOffsetSameInstant(ZoneOffset.UTC);
    System.out.println(l);//w  w w .ja  v  a2s  . co  m
}

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.silverpeas.core.calendar.CalendarEventOccurrenceGenerationTest.java

@Test
public void nextOccurrenceAboutRecurrentHourEventStartingOnSummerShouldWork() {
    final OffsetDateTime startDateTimeOnParis = dateTimeOnParis(2017, 7, 11, 23, 0);
    final OffsetDateTime endDateTimeOnParis = dateTimeOnParis(2017, 7, 12, 0, 45);
    assertThat(startDateTimeOnParis.withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 7, 11, 21, 0)));
    assertThat(endDateTimeOnParis.withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 7, 11, 22, 45)));
    assertThat(dateTimeOnParis(2017, 12, 11, 23, 0).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 12, 11, 22, 0)));
    assertThat(dateTimeOnParis(2017, 12, 12, 0, 45).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 12, 11, 23, 45)));
    CalendarEvent recurrentEvent = calendarEventForTest(
            Period.between(startDateTimeOnParis, endDateTimeOnParis), PARIS_ZONE_ID)
                    .recur(Recurrence.every(1, TimeUnit.MONTH).until(10));
    ZonedDateTime from = ZonedDateTime.parse("2017-12-11T21:59:59-01:00[Atlantic/Azores]");
    CalendarEventOccurrence result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());/*from   w  w  w . j a  v  a  2s  . c  om*/
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 11, 23, 45)));

    from = ZonedDateTime.parse("2017-12-11T22:00:00-01:00[Atlantic/Azores]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 11, 23, 45)));

    from = ZonedDateTime.parse("2017-12-11T22:59:59+00:00[UTC]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 11, 23, 45)));

    from = ZonedDateTime.parse("2017-12-11T23:00:00+00:00[UTC]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 11, 23, 45)));

    from = ZonedDateTime.parse("2017-12-11T22:59:59+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 12, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 12, 11, 23, 45)));

    from = ZonedDateTime.parse("2017-12-11T23:00:00+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 11, 22, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 11, 23, 45)));
}

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

@Test
public void nextOccurrenceAboutRecurrentHourEventStartingOnSummerAndNowAboutHourChangingShouldWork() {
    final OffsetDateTime startDateTimeOnParis = dateTimeOnParis(2017, 7, 29, 3, 0);
    final OffsetDateTime endDateTimeOnParis = dateTimeOnParis(2017, 7, 29, 4, 30);
    assertThat(startDateTimeOnParis.withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 7, 29, 1, 0)));
    assertThat(endDateTimeOnParis.withOffsetSameInstant(ZoneOffset.UTC), is(dateTimeInUTC(2017, 7, 29, 2, 30)));
    assertThat(dateTimeOnParis(2017, 10, 28, 23, 59).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 10, 28, 21, 59)));
    assertThat(dateTimeOnParis(2017, 10, 29, 0, 0).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 10, 28, 22, 0)));
    assertThat(dateTimeOnParis(2017, 10, 29, 2, 0).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 10, 29, 0, 0)));
    assertThat(dateTimeOnParis(2017, 10, 29, 2, 59).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 10, 29, 0, 59)));
    assertThat(dateTimeOnParis(2017, 10, 29, 3, 0).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2017, 10, 29, 2, 0)));
    assertThat(dateTimeOnParis(2018, 3, 25, 2, 59).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2018, 3, 25, 1, 59)));
    assertThat(dateTimeOnParis(2018, 3, 25, 3, 0).withOffsetSameInstant(ZoneOffset.UTC),
            is(dateTimeInUTC(2018, 3, 25, 1, 0)));
    CalendarEvent recurrentEvent = calendarEventForTest(
            Period.between(startDateTimeOnParis, endDateTimeOnParis), PARIS_ZONE_ID)
                    .recur(Recurrence.every(1, TimeUnit.MONTH).until(100));
    ZonedDateTime from = ZonedDateTime.parse("2017-10-29T00:59:59-01:00[Atlantic/Azores]");
    CalendarEventOccurrence result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());/* w w  w .  j av a2s .  c o  m*/
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 10, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 10, 29, 3, 30)));

    from = ZonedDateTime.parse("2017-10-29T01:00:00-01:00[Atlantic/Azores]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 11, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 11, 29, 3, 30)));

    from = ZonedDateTime.parse("2017-10-29T01:59:59+00:00[UTC]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 10, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 10, 29, 3, 30)));

    from = ZonedDateTime.parse("2017-10-29T02:00:00+00:00[UTC]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 11, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 11, 29, 3, 30)));

    from = ZonedDateTime.parse("2017-10-29T02:59:59+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 10, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 10, 29, 3, 30)));

    from = ZonedDateTime.parse("2017-10-29T03:00:00+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2017, 11, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2017, 11, 29, 3, 30)));

    from = ZonedDateTime.parse("2018-01-29T02:59:59+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 1, 29, 2, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 1, 29, 3, 30)));

    from = ZonedDateTime.parse("2018-01-29T03:00:00+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 3, 29, 1, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 3, 29, 2, 30)));

    from = ZonedDateTime.parse("2018-02-28T02:59:59+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 3, 29, 1, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 3, 29, 2, 30)));

    from = ZonedDateTime.parse("2018-02-28T03:00:00+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 3, 29, 1, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 3, 29, 2, 30)));

    from = ZonedDateTime.parse("2018-03-29T02:59:59+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 3, 29, 1, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 3, 29, 2, 30)));

    from = ZonedDateTime.parse("2018-03-29T03:00:00+01:00[Europe/Paris]");
    result = generator.generateNextOccurrenceOf(recurrentEvent, from);
    assertThat(result, notNullValue());
    assertThat(result.getStartDate(), is(dateTimeInUTC(2018, 4, 29, 1, 0)));
    assertThat(result.getEndDate(), is(dateTimeInUTC(2018, 4, 29, 2, 30)));
}

From source file:org.silverpeas.core.date.Period.java

/**
 * Creates a new period of time between the two non null specified datetime. The period starts at
 * the specified inclusive datetime and it ends at the specified other exclusive datetime. For
 * example, a period between 2016-12-17T13:30:00Z and 2016-12-17T14:30:00Z means the period is
 * spanning one hour the December 12.//from   ww w. j  ava2 s.  c  om
 * @param startDateTime the start datetime of the period. It defines the inclusive date
 * time at which the period starts.
 * @param endDateTime the end datetime of the period. It defines the exclusive datetime
 * at which the period ends. The end datetime must be after the start datetime.
 * @return the period of time between the two specified datetimes.
 */
public static Period between(OffsetDateTime startDateTime, OffsetDateTime endDateTime) {
    checkPeriod(startDateTime, endDateTime);
    Period period = new Period();
    period.startDateTime = startDateTime == OffsetDateTime.MIN ? OffsetDateTime.MIN
            : startDateTime.withOffsetSameInstant(ZoneOffset.UTC);
    period.endDateTime = endDateTime == OffsetDateTime.MAX ? OffsetDateTime.MAX
            : endDateTime.withOffsetSameInstant(ZoneOffset.UTC);
    period.inDays = false;
    return period;
}