java.time.OffsetDateTime#ofInstant ( )源码实例Demo

下面列出了java.time.OffsetDateTime#ofInstant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: oxygen   文件: Java8DatePropertyHandler.java
@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;
}
 
源代码2 项目: jdk8u-jdk   文件: TestOffsetDateTime_instants.java
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;
        }
    }
}
 
源代码3 项目: jdk8u_jdk   文件: TestOffsetDateTime_instants.java
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);
    }
}
 
@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);
}
 
@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);
}
 
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);
    }
}
 
@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());
}
 
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);
}
 
源代码9 项目: OAuth-2.0-Cookbook   文件: OpenIDAuthentication.java
public boolean hasExpired() {
    OffsetDateTime expirationDateTime = OffsetDateTime.ofInstant(
            Instant.ofEpochSecond(expirationTime), ZoneId.systemDefault());

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

    return now.isAfter(expirationDateTime);
}
 
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);
}
 
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);
}
 
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);
}
 
源代码13 项目: openjdk-jdk9   文件: TestOffsetDateTime_instants.java
@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);
}
 
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullOffset() {
    Instant instant = Instant.ofEpochSecond(0L);
    OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
}
 
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_maxInstantWithMaxOffset() {
    Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
    OffsetDateTime.ofInstant(instant, OFFSET_MAX);
}
 
源代码16 项目: hottub   文件: TestOffsetDateTime_instants.java
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
源代码17 项目: jdk8u-jdk   文件: TestOffsetDateTime_instants.java
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullOffset() {
    Instant instant = Instant.ofEpochSecond(0L);
    OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
}
 
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
    OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
}
 
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_maxInstantWithMinOffset() {
    Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
    OffsetDateTime.ofInstant(instant, OFFSET_MIN);
}