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

The following examples show how to use java.time.ZonedDateTime#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: WemoCoffeeHandler.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
public State getDateTimeState(String attributeValue) {
    if (attributeValue != null) {
        long value = 0;
        try {
            value = Long.parseLong(attributeValue) * 1000; // convert s to ms
        } catch (NumberFormatException e) {
            logger.error("Unable to parse attributeValue '{}' for device '{}'; expected long", attributeValue,
                    getThing().getUID());
            return null;
        }
        ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value),
                TimeZone.getDefault().toZoneId());
        State dateTimeState = new DateTimeType(zoned);
        if (dateTimeState != null) {
            logger.trace("New attribute brewed '{}' received", dateTimeState);
            return dateTimeState;
        }
    }
    return null;
}
 
Example 2
Source File: TCKZonedDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void factory_ofInstant_maxWithMaxOffset() {
    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_MAX.getTotalSeconds());
    ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MAX);
    assertEquals(test.getYear(), Year.MAX_VALUE);
    assertEquals(test.getMonth().getValue(), 12);
    assertEquals(test.getDayOfMonth(), 31);
    assertEquals(test.getOffset(), OFFSET_MAX);
    assertEquals(test.getHour(), 23);
    assertEquals(test.getMinute(), 59);
    assertEquals(test.getSecond(), 59);
    assertEquals(test.getNano(), 0);
}
 
Example 3
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allSecsInDay() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_0100);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 4
Source File: InsightsUtils.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates difference between nextRunTime and lastRunTime (nextRunTime -
 * lastRunTime )
 * 
 * @param lastRunTime
 * @param nextRunTime
 * @return
 */
public static long getDifferenceFromNextRunTime(Long lastRunTime, Long nextRunTime) {
	ZonedDateTime lastRunTimeInput = ZonedDateTime.ofInstant(Instant.ofEpochSecond(lastRunTime),
			InsightsUtils.zoneId);
	ZonedDateTime nextRunTimeInput = ZonedDateTime.ofInstant(Instant.ofEpochSecond(nextRunTime),
			InsightsUtils.zoneId);
	Duration d = Duration.between(lastRunTimeInput, nextRunTimeInput);
	return d.abs().toMillis();
}
 
Example 5
Source File: TestZonedDateTimeSerialization.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationAsString02() throws Exception
{
    ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2);
    String value = MAPPER.writer()
            .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .writeValueAsString(date);
    assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value);
}
 
Example 6
Source File: TCKZoneRules.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void test_London_previousTransition_rulesBased() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransitionRule> rules = test.getTransitionRules();
    List<ZoneOffsetTransition> trans = test.getTransitions();

    ZoneOffsetTransition last = trans.get(trans.size() - 1);
    assertEquals(test.previousTransition(last.getInstant().plusSeconds(1)), last);
    assertEquals(test.previousTransition(last.getInstant().plusNanos(1)), last);

    // Jan 1st of year between transitions and rules
    ZonedDateTime odt = ZonedDateTime.ofInstant(last.getInstant(), last.getOffsetAfter());
    odt = odt.withDayOfYear(1).plusYears(1).with(LocalTime.MIDNIGHT);
    assertEquals(test.previousTransition(odt.toInstant()), last);

    // later years
    for (int year = 1998; year < 2010; year++) {
        ZoneOffsetTransition a = rules.get(0).createTransition(year);
        ZoneOffsetTransition b = rules.get(1).createTransition(year);
        ZoneOffsetTransition c = rules.get(0).createTransition(year + 1);

        assertEquals(test.previousTransition(c.getInstant()), b);
        assertEquals(test.previousTransition(b.getInstant().plusSeconds(1)), b);
        assertEquals(test.previousTransition(b.getInstant().plusNanos(1)), b);

        assertEquals(test.previousTransition(b.getInstant()), a);
        assertEquals(test.previousTransition(a.getInstant().plusSeconds(1)), a);
        assertEquals(test.previousTransition(a.getInstant().plusNanos(1)), a);
    }
}
 
Example 7
Source File: TestZonedDateTimeSerialization.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializationAsString02WithoutTimeZone() throws Exception
{
    ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(123456789L, 183917322), Z2);
    ZonedDateTime value = READER
            .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
            .readValue('"' + FORMATTER.format(date) + '"');

    assertNotNull("The value should not be null.", value);
    assertIsEqual(date, value);
    assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone());
}
 
Example 8
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 9
Source File: DateTimeUtils.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public static String formatMilliToString(Long milli) {
  if (milli == null) {
    return "";
  }
  Instant instant = Instant.ofEpochMilli(milli);
  ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, java.time.ZoneId.of("UTC"));
  return zonedDateTime.format(simple);
}
 
Example 10
Source File: WeatherUndergroundJsonUtils.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert a string representing an Epoch value into a Calendar object
 *
 * @param value the Epoch value as a string
 *
 * @return the ZonedDateTime object representing the date and time of the Epoch
 *         or null in case of conversion error
 */
public static ZonedDateTime convertToZonedDateTime(String value) {
    if (isValid(value)) {
        try {
            Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value));
            return ZonedDateTime.ofInstant(epochSeconds, TimeZone.getDefault().toZoneId());
        } catch (DateTimeException e) {
            LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime",
                    value);
        }
    }

    return null;
}
 
Example 11
Source File: DenseArrayOfZonedDateTimes.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final ZonedDateTime getValue(int index) {
    final long value = values[index];
    if (value == nullValue) {
        return null;
    } else {
        final ZoneId zone = zoneIdMap2.get(zoneIds[index]);
        final Instant instant = Instant.ofEpochMilli(value);
        return ZonedDateTime.ofInstant(instant, zone);
    }
}
 
Example 12
Source File: JSR310DateConverters.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@Override
public ZonedDateTime convert(Date source) {
    return source == null ? null : ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
 
Example 13
Source File: TCKZonedDateTime.java    From jdk8u_jdk 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);
    ZonedDateTime.ofInstant(instant, OFFSET_MAX);
}
 
Example 14
Source File: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_ofInstant_Instant_inOverlap_earlier() {
    Instant instant = TEST_PARIS_OVERLAP_2008_10_26_02_30.toInstant(OFFSET_0200);
    ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS);
    check(test, 2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS);  // same time and offset
}
 
Example 15
Source File: ZonedDateTimeTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = NullPointerException.class)
public void test_ofInstant_offset_null() {
    ZonedDateTime.ofInstant(LDT_P1, null, ZONE_VIENNA);
}
 
Example 16
Source File: ZonedDateTimeAttributeDescription.java    From constellation with Apache License 2.0 4 votes vote down vote up
@Override
public void setLong(final int id, final long value) {
    data[id] = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), TimeZoneUtilities.UTC);
}
 
Example 17
Source File: TCKZonedDateTime.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);
    ZonedDateTime.ofInstant(instant, OFFSET_MAX);
}
 
Example 18
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_ofInstant_Instant_inGap() {
    Instant instant = TEST_PARIS_GAP_2008_03_30_02_30.toInstant(OFFSET_0100);
    ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS);
    check(test, 2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS);  // one hour later in summer offset
}
 
Example 19
Source File: PyishDate.java    From jinjava with Apache License 2.0 4 votes vote down vote up
public PyishDate(Instant instant) {
  this(ZonedDateTime.ofInstant(instant, ZoneOffset.UTC));
}
 
Example 20
Source File: GregorianCalendar.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Converts this object to a {@code ZonedDateTime} that represents
 * the same point on the time-line as this {@code GregorianCalendar}.
 * <p>
 * Since this object supports a Julian-Gregorian cutover date and
 * {@code ZonedDateTime} does not, it is possible that the resulting year,
 * month and day will have different values.  The result will represent the
 * correct date in the ISO calendar system, which will also be the same value
 * for Modified Julian Days.
 *
 * @return a zoned date-time representing the same point on the time-line
 *  as this gregorian calendar
 * @since 1.8
 */
public ZonedDateTime toZonedDateTime() {
    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getTimeInMillis()),
                                   getTimeZone().toZoneId());
}