Java Code Examples for java.time.OffsetDateTime#ofInstant()

The following examples show how to use java.time.OffsetDateTime#ofInstant() . 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: TestOffsetDateTime_instants.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void doTest_factory_ofInstant_all(long minYear, long maxYear) {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int minOffset = (minYear <= 0 ? 0 : 3);
    int maxOffset = (maxYear <= 0 ? 0 : 3);
    long minDays = (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
    long maxDays = (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;

    final LocalDate maxDate = LocalDate.of(Year.MAX_VALUE, 12, 31);
    OffsetDateTime expected = OffsetDateTime.of(LocalDate.of((int) minYear, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
    for (long i = minDays; i < maxDays; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        try {
            OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
            assertEquals(test, expected);
            if (expected.toLocalDate().equals(maxDate) == false) {
                expected = expected.plusDays(1);
            }
        } catch (RuntimeException|Error ex) {
            System.out.println("Error: " + i + " " + expected);
            throw ex;
        }
    }
}
 
Example 2
Source File: Java8DatePropertyHandler.java    From oxygen with Apache License 2.0 6 votes vote down vote up
@Override
public Object cast(Class<?> type, Object value) {
  Timestamp timestamp = new Timestamp(((Date) value).getTime());
  if (type == Instant.class) {
    value = timestamp.toInstant();
  } else if (type == JapaneseDate.class) {
    value = JapaneseDate.from(timestamp.toLocalDateTime());
  } else if (type == LocalDateTime.class) {
    value = timestamp.toLocalDateTime();
  } else if (type == LocalDate.class) {
    value = timestamp.toLocalDateTime().toLocalDate();
  } else if (type == LocalTime.class) {
    value = timestamp.toLocalDateTime().toLocalTime();
  } else if (type == OffsetDateTime.class) {
    value = OffsetDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
  } else if (type == OffsetTime.class) {
    value = timestamp.toLocalDateTime().atOffset(OffsetDateTime.now().getOffset()).toOffsetTime();
  } else if (type == ZonedDateTime.class) {
    value = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
  }
  return value;
}
 
Example 3
Source File: TestOffsetDateTime_instants.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE - 1;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
    OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
 
Example 4
Source File: TestOffsetDateTime_instants.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE - 1;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
    OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
 
Example 5
Source File: TestOffsetDateTime_instants.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_maxWithMinOffset() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MAX_VALUE;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds());
    OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
    assertEquals(test.getYear(), Year.MAX_VALUE);
    assertEquals(test.getMonth().getValue(), 12);
    assertEquals(test.getDayOfMonth(), 31);
    assertEquals(test.getOffset(), OFFSET_MIN);
    assertEquals(test.getHour(), 23);
    assertEquals(test.getMinute(), 59);
    assertEquals(test.getSecond(), 59);
    assertEquals(test.getNano(), 0);
}
 
Example 6
Source File: TestOffsetDateTime_instants.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_maxWithMinOffset() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MAX_VALUE;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds());
    OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
    assertEquals(test.getYear(), Year.MAX_VALUE);
    assertEquals(test.getMonth().getValue(), 12);
    assertEquals(test.getDayOfMonth(), 31);
    assertEquals(test.getOffset(), OFFSET_MIN);
    assertEquals(test.getHour(), 23);
    assertEquals(test.getMinute(), 59);
    assertEquals(test.getSecond(), 59);
    assertEquals(test.getNano(), 0);
}
 
Example 7
Source File: TestOffsetDateTime_instants.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_minWithMaxOffset() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MAX.getTotalSeconds());
    OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MAX);
    assertEquals(test.getYear(), Year.MIN_VALUE);
    assertEquals(test.getMonth().getValue(), 1);
    assertEquals(test.getDayOfMonth(), 1);
    assertEquals(test.getOffset(), OFFSET_MAX);
    assertEquals(test.getHour(), 0);
    assertEquals(test.getMinute(), 0);
    assertEquals(test.getSecond(), 0);
    assertEquals(test.getNano(), 0);
}
 
Example 8
Source File: OpenIDAuthentication.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
public boolean hasExpired() {
    OffsetDateTime expirationDateTime = OffsetDateTime.ofInstant(
            Instant.ofEpochSecond(expirationTime), ZoneId.systemDefault());

    OffsetDateTime now = OffsetDateTime.now(ZoneId.systemDefault());

    return now.isAfter(expirationDateTime);
}
 
Example 9
Source File: TestOffsetDateTime_instants.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_minWithMinOffset() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MIN.getTotalSeconds());
    OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
    assertEquals(test.getYear(), Year.MIN_VALUE);
    assertEquals(test.getMonth().getValue(), 1);
    assertEquals(test.getDayOfMonth(), 1);
    assertEquals(test.getOffset(), OFFSET_MIN);
    assertEquals(test.getHour(), 0);
    assertEquals(test.getMinute(), 0);
    assertEquals(test.getSecond(), 0);
    assertEquals(test.getNano(), 0);
}
 
Example 10
Source File: OffsetDateTimeRangeRandomizer.java    From easy-random with MIT License 5 votes vote down vote up
@Override
public OffsetDateTime getRandomValue() {
    long minSeconds = min.toEpochSecond();
    long maxSeconds = max.toEpochSecond();
    long seconds = (long) nextDouble(minSeconds, maxSeconds);
    int minNanoSeconds = min.getNano();
    int maxNanoSeconds = max.getNano();
    long nanoSeconds = (long) nextDouble(minNanoSeconds, maxNanoSeconds);
    return OffsetDateTime.ofInstant(Instant.ofEpochSecond(seconds, nanoSeconds),
            EasyRandomParameters.DEFAULT_DATES_RANGE.getMin().getZone());
}
 
Example 11
Source File: TestOffsetDateTime_instants.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 12
Source File: OffsetDateTimeSerTest.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationAsTimestamp01Nanoseconds() throws Exception
{
    OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1);
    String value = MAPPER.writer()
            .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
            .writeValueAsString(date);
    assertEquals("The value is not correct.", "0.0", value);
}
 
Example 13
Source File: TestOffsetDateTime_instants.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 14
Source File: TestOffsetDateTime_instants.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_maxInstantWithMaxOffset() {
    Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
    OffsetDateTime.ofInstant(instant, OFFSET_MAX);
}
 
Example 15
Source File: TestOffsetDateTime_instants.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullOffset() {
    Instant instant = Instant.ofEpochSecond(0L);
    OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
}
 
Example 16
Source File: TestOffsetDateTime_instants.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
Example 17
Source File: TestOffsetDateTime_instants.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
Example 18
Source File: TestOffsetDateTime_instants.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullOffset() {
    Instant instant = Instant.ofEpochSecond(0L);
    OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
}
 
Example 19
Source File: TestOffsetDateTime_instants.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
Example 20
Source File: TestOffsetDateTime_instants.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_maxInstantWithMinOffset() {
    Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
    OffsetDateTime.ofInstant(instant, OFFSET_MIN);
}