Java Code Examples for java.time.Clock#fixed()

The following examples show how to use java.time.Clock#fixed() . 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
Source File: TCKZonedDateTime.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
    LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
    for (int i =-1; i >= -(24 * 60 * 60); i--) {
        Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        ZonedDateTime test = ZonedDateTime.now(clock);
        assertEquals(test.getYear(), 1969);
        assertEquals(test.getMonth(), Month.DECEMBER);
        assertEquals(test.getDayOfMonth(), 31);
        expected = expected.minusSeconds(1);
        assertEquals(test.toLocalTime(), expected);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
        assertEquals(test.getZone(), ZoneOffset.UTC);
    }
}
 
Example 2
Source File: TCKLocalDate.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_utc() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        LocalDate test = LocalDate.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
    }
}
 
Example 3
Source File: TCKLocalTime.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i, 8);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        LocalTime test = LocalTime.now(clock);
        assertEquals(test.getHour(), (i / (60 * 60)) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
        assertEquals(test.getNano(), 8);
    }
}
 
Example 4
Source File: TCKLocalDate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_offset() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE);
        LocalDate test = LocalDate.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
    }
}
 
Example 5
Source File: TCKOffsetTime.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i, 8);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        OffsetTime test = OffsetTime.now(clock);
        assertEquals(test.getHour(), (i / (60 * 60)) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
        assertEquals(test.getNano(), 8);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
    }
}
 
Example 6
Source File: TCKOffsetTime.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_offsets() {
    Instant base = LocalDateTime.of(1970, 1, 1, 12, 0).toInstant(ZoneOffset.UTC);
    for (int i = -9; i < 15; i++) {
        ZoneOffset offset = ZoneOffset.ofHours(i);
        Clock clock = Clock.fixed(base, offset);
        OffsetTime test = OffsetTime.now(clock);
        assertEquals(test.getHour(), (12 + i) % 24);
        assertEquals(test.getMinute(), 0);
        assertEquals(test.getSecond(), 0);
        assertEquals(test.getNano(), 0);
        assertEquals(test.getOffset(), offset);
    }
}
 
Example 7
Source File: TCKMonthDay.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock() {
    Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC);
    Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
    MonthDay test = MonthDay.now(clock);
    assertEquals(test.getMonth(), Month.DECEMBER);
    assertEquals(test.getDayOfMonth(), 31);
}
 
Example 8
Source File: TCKOffsetTime.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i, 8);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        OffsetTime test = OffsetTime.now(clock);
        assertEquals(test.getHour(), (i / (60 * 60)) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
        assertEquals(test.getNano(), 8);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
    }
}
 
Example 9
Source File: TCKOffsetDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
    LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
    for (int i =-1; i >= -(24 * 60 * 60); i--) {
        Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getYear(), 1969);
        assertEquals(test.getMonth(), Month.DECEMBER);
        assertEquals(test.getDayOfMonth(), 31);
        expected = expected.minusSeconds(1);
        assertEquals(test.toLocalTime(), expected);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
    }
}
 
Example 10
Source File: TCKInstant.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
    for (int i =-1; i >= -(24 * 60 * 60); i--) {
        Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(expected, ZoneOffset.UTC);
        Instant test = Instant.now(clock);
        assertEquals(test, expected);
    }
}
 
Example 11
Source File: TCKClock_Fixed.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void test_hashCode() {
    Clock a = Clock.fixed(INSTANT, ZoneOffset.UTC);
    Clock b = Clock.fixed(INSTANT, ZoneOffset.UTC);
    assertEquals(a.hashCode(), a.hashCode());
    assertEquals(a.hashCode(), b.hashCode());

    Clock c = Clock.fixed(INSTANT, PARIS);
    assertEquals(a.hashCode() == c.hashCode(), false);

    Clock d = Clock.fixed(INSTANT.minusNanos(1), ZoneOffset.UTC);
    assertEquals(a.hashCode() == d.hashCode(), false);
}
 
Example 12
Source File: TCKInstant.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
    for (int i =-1; i >= -(24 * 60 * 60); i--) {
        Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(expected, ZoneOffset.UTC);
        Instant test = Instant.now(clock);
        assertEquals(test, expected);
    }
}
 
Example 13
Source File: TCKLocalTime.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_min() {
    Clock clock = Clock.fixed(Instant.MIN, ZoneOffset.UTC);
    LocalTime test = LocalTime.now(clock);
    assertEquals(test.getHour(), 0);
    assertEquals(test.getMinute(), 0);
    assertEquals(test.getSecond(), 0);
    assertEquals(test.getNano(), 0);
}
 
Example 14
Source File: TestClock_Fixed.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void test_withZone_same() {
    Clock test = Clock.fixed(INSTANT, PARIS);
    Clock changed = test.withZone(PARIS);
    assertSame(test, changed);
}
 
Example 15
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void now_Clock_minYear() {
    Clock clock = Clock.fixed(MIN_INSTANT, ZoneOffset.UTC);
    LocalDate test = LocalDate.now(clock);
    assertEquals(test, MIN_DATE);
}
 
Example 16
Source File: TCKLocalDate.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void now_Clock_tooLow() {
    Clock clock = Clock.fixed(MIN_INSTANT.minusNanos(1), ZoneOffset.UTC);
    LocalDate.now(clock);
}
 
Example 17
Source File: TCKClock_Fixed.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void test_fixed_InstantZoneId_nullZoneId() {
    Clock.fixed(INSTANT, null);
}
 
Example 18
Source File: TCKLocalDate.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void now_Clock_tooBig() {
    Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
    LocalDate.now(clock);
}
 
Example 19
Source File: TCKClock_Fixed.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void test_withZone_equal() {
    Clock test = Clock.fixed(INSTANT, PARIS);
    Clock changed = test.withZone(PARIS);
    assertEquals(changed.getZone(), PARIS);
}
 
Example 20
Source File: TCKLocalDate.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void now_Clock_maxYear() {
    Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC);
    LocalDate test = LocalDate.now(clock);
    assertEquals(test, MAX_DATE);
}