Java Code Examples for java.time.LocalDateTime#plusHours()

The following examples show how to use java.time.LocalDateTime#plusHours() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 2
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 3
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 4
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 5
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 6
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 7
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 8
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 9
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 10
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 11
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 12
Source Project: j2objc   File: TCKLocalDateTime.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 13
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 14
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 15
@Test
public void test_plusHours_fromOne() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(22, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);

        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 16
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 17
@Test
public void test_plusHours_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    for (int i = 0; i < 50; i++) {
        t = t.plusHours(1);

        if ((i + 1) % 24 == 0) {
            d = d.plusDays(1);
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), (i + 1) % 24);
    }
}
 
Example 18
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 19
public LocalDateTime updateHour(LocalDateTime datetime, String hour) {
  if (!Strings.isNullOrEmpty(hour)) {
    Matcher matcher = patternMonth.matcher(hour);
    if (matcher.find()) {
      Long hours = Long.parseLong(matcher.group());
      if (hour.startsWith("+")) datetime = datetime.plusHours(hours);
      else if (hour.startsWith("-")) datetime = datetime.minusHours(hours);
      else datetime = datetime.withHour(hours.intValue());
    }
  }
  return datetime;
}
 
Example 20
Source Project: tutorials   File: JavaUtilTimeUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void getTimeSpan() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime hourLater = now.plusHours(1);
    Duration span = Duration.between(now, hourLater);

    assertThat(span).isEqualTo(Duration.ofHours(1));
}