Java Code Examples for java.time.temporal.TemporalAccessor#getLong()

The following examples show how to use java.time.temporal.TemporalAccessor#getLong() . 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: Parsed.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
private void crossCheck(TemporalAccessor target) {
    for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
        Entry<TemporalField, Long> entry = it.next();
        TemporalField field = entry.getKey();
        if (target.isSupported(field)) {
            long val1;
            try {
                val1 = target.getLong(field);
            } catch (RuntimeException ex) {
                continue;
            }
            long val2 = entry.getValue();
            if (val1 != val2) {
                throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
                        " differs from " + field + " " + val2 + " derived from " + target);
            }
            it.remove();
        }
    }
}
 
Example 2
Source File: Parsed.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
private void crossCheck(TemporalAccessor target) {
    for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
        Entry<TemporalField, Long> entry = it.next();
        TemporalField field = entry.getKey();
        if (target.isSupported(field)) {
            long val1;
            try {
                val1 = target.getLong(field);
            } catch (RuntimeException ex) {
                continue;
            }
            long val2 = entry.getValue();
            if (val1 != val2) {
                throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
                        " differs from " + field + " " + val2 + " derived from " + target);
            }
            it.remove();
        }
    }
}
 
Example 3
Source File: Parsed.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void crossCheck(TemporalAccessor target) {
    for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {
        Entry<TemporalField, Long> entry = it.next();
        TemporalField field = entry.getKey();
        if (target.isSupported(field)) {
            long val1;
            try {
                val1 = target.getLong(field);
            } catch (RuntimeException ex) {
                continue;
            }
            long val2 = entry.getValue();
            if (val1 != val2) {
                throw new DateTimeException("Conflict found: Field " + field + " " + val1 +
                        " differs from " + field + " " + val2 + " derived from " + target);
            }
            it.remove();
        }
    }
}
 
Example 4
Source File: AbstractDateTimeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void basicTest_getLong_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example 5
Source File: AbstractDateTimeTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void basicTest_getLong_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example 6
Source File: AttributeValueConverter.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private Date convertToDate(TemporalAccessor t)
{
    if(!t.isSupported(ChronoField.INSTANT_SECONDS))
    {
        t = LocalDateTime.of(LocalDate.from(t), LocalTime.MIN).atOffset(ZoneOffset.UTC);
    }
    return new Date((t.getLong(ChronoField.INSTANT_SECONDS) * 1000L)
                     + t.getLong(ChronoField.MILLI_OF_SECOND));


}
 
Example 7
Source File: AbstractDateTimeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void basicTest_getLong_TemporalField_null() {
    for (TemporalAccessor sample : samples()) {
        try {
            sample.getLong(null);
            fail("Failed on " + sample);
        } catch (NullPointerException ex) {
            // expected
        }
    }
}
 
Example 8
Source File: TCKDateTimeFormatters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void assertParseMatch(TemporalAccessor parsed, Expected expected) {
    for (TemporalField field : expected.fieldValues.keySet()) {
        assertEquals(parsed.isSupported(field), true);
        parsed.getLong(field);
    }
    assertEquals(parsed.query(TemporalQueries.chronology()), expected.chrono);
    assertEquals(parsed.query(TemporalQueries.zoneId()), expected.zone);
}
 
Example 9
Source File: DateUtility.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Best effort parsing of the given value
 */
public static LocalDateTime parseBest(String value) {
  TemporalAccessor parsed = getDateTimeFormatter().parse(value);
  LocalDate datePart;
  LocalTime timePart;
  ZoneOffset zoneOffset;

  long epochDay = 0, nanoSeconds = 0;
  int offsetSeconds = 0;

  // get different parsed parts
  if (parsed.isSupported(ChronoField.EPOCH_DAY)) {
    epochDay = parsed.getLong(ChronoField.EPOCH_DAY);
  }
  if (parsed.isSupported(ChronoField.NANO_OF_DAY)) {
    nanoSeconds = parsed.getLong(ChronoField.NANO_OF_DAY);
  }
  if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) {
    offsetSeconds = parsed.get(ChronoField.OFFSET_SECONDS);
  }

  zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
  datePart = LocalDate.ofEpochDay(epochDay);
  timePart = LocalTime.ofNanoOfDay(nanoSeconds);

  return OffsetDateTime.of(datePart, timePart, zoneOffset).toLocalDateTime();
}
 
Example 10
Source File: AbstractDateTimeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void basicTest_getLong_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example 11
Source File: TCKDateTimeFormatters.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void assertParseMatch(TemporalAccessor parsed, Expected expected) {
    for (TemporalField field : expected.fieldValues.keySet()) {
        assertEquals(parsed.isSupported(field), true);
        parsed.getLong(field);
    }
    assertEquals(parsed.query(TemporalQueries.chronology()), expected.chrono);
    assertEquals(parsed.query(TemporalQueries.zoneId()), expected.zone);
}
 
Example 12
Source File: TCKDateTimeFormatters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void assertParseMatch(TemporalAccessor parsed, Expected expected) {
    for (TemporalField field : expected.fieldValues.keySet()) {
        assertEquals(parsed.isSupported(field), true);
        parsed.getLong(field);
    }
    assertEquals(parsed.query(TemporalQueries.chronology()), expected.chrono);
    assertEquals(parsed.query(TemporalQueries.zoneId()), expected.zone);
}
 
Example 13
Source File: AbstractDateTimeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_getLong_TemporalField_invalidField() {
    for (TemporalAccessor sample : samples()) {
        sample.getLong(MockFieldNoValue.INSTANCE);
    }
}
 
Example 14
Source File: ConfiguredObjectExpressionFactory.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
ConfiguredObjectExpression asExpression(final List<Expression> args)
{
    if (args == null || args.size() != 1)
    {
        throw new IllegalArgumentException(TO_DATE.name() + " requires a single argument.");
    }

    return new ConfiguredObjectExpression()
    {
        @Override
        public Object evaluate(final ConfiguredObject<?> object)
        {
            Object dateTime = args.get(0).evaluate(object);
            if (!(dateTime instanceof String))
            {
                throw new IllegalArgumentException(TO_DATE.name() + " requires a string argument, not a " + dateTime.getClass());
            }
            try
            {

                return DateTimeFormatter.ISO_ZONED_DATE_TIME.parse((String)dateTime)
                        .query(this::convertToDate);
            }
            catch (DateTimeParseException e1)
            {
                throw new IllegalArgumentException(TO_DATE
                                                   + " requires an ISO-8601 format date or date/time.",
                                                   e1);
            }

        }

        private Date convertToDate(TemporalAccessor t)
        {
            if(!t.isSupported(ChronoField.INSTANT_SECONDS))
            {
                t = LocalDateTime.of(LocalDate.from(t), LocalTime.MIN).atOffset(ZoneOffset.UTC);
            }
            return new Date((t.getLong(ChronoField.INSTANT_SECONDS) * 1000L)
                            + t.getLong(ChronoField.MILLI_OF_SECOND));
            
        }
    };
}
 
Example 15
Source File: ZonedDateTime.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a temporal object.
 * <p>
 * This obtains a zoned date-time based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code LocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code LocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code ZonedDateTime::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if unable to convert to an {@code ZonedDateTime}
 */
public static ZonedDateTime from(TemporalAccessor temporal) {
    if (temporal instanceof ZonedDateTime) {
        return (ZonedDateTime) temporal;
    }
    try {
        ZoneId zone = ZoneId.from(temporal);
        if (temporal.isSupported(INSTANT_SECONDS)) {
            long epochSecond = temporal.getLong(INSTANT_SECONDS);
            int nanoOfSecond = temporal.get(NANO_OF_SECOND);
            return create(epochSecond, nanoOfSecond, zone);
        } else {
            LocalDate date = LocalDate.from(temporal);
            LocalTime time = LocalTime.from(temporal);
            return of(date, time, zone);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName(), ex);
    }
}
 
Example 16
Source File: ZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a temporal object.
 * <p>
 * This obtains a zoned date-time based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code ZonedDateTime}.
 * <p>
 * The conversion will first obtain a {@code ZoneId} from the temporal object,
 * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
 * an {@code Instant}, falling back to a {@code LocalDateTime} if necessary.
 * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
 * with {@code Instant} or {@code LocalDateTime}.
 * Implementations are permitted to perform optimizations such as accessing
 * those fields that are equivalent to the relevant objects.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code ZonedDateTime::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if unable to convert to an {@code ZonedDateTime}
 */
public static ZonedDateTime from(TemporalAccessor temporal) {
    if (temporal instanceof ZonedDateTime) {
        return (ZonedDateTime) temporal;
    }
    try {
        ZoneId zone = ZoneId.from(temporal);
        if (temporal.isSupported(INSTANT_SECONDS)) {
            long epochSecond = temporal.getLong(INSTANT_SECONDS);
            int nanoOfSecond = temporal.get(NANO_OF_SECOND);
            return create(epochSecond, nanoOfSecond, zone);
        } else {
            LocalDate date = LocalDate.from(temporal);
            LocalTime time = LocalTime.from(temporal);
            return of(date, time, zone);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName(), ex);
    }
}
 
Example 17
Source File: AbstractDateTimeTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_getLong_TemporalField_invalidField() {
    for (TemporalAccessor sample : samples()) {
        sample.getLong(MockFieldNoValue.INSTANCE);
    }
}
 
Example 18
Source File: Instant.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code Instant} from a temporal object.
 * <p>
 * This obtains an instant based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code Instant}.
 * <p>
 * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code Instant::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the instant, not null
 * @throws DateTimeException if unable to convert to an {@code Instant}
 */
public static Instant from(TemporalAccessor temporal) {
    if (temporal instanceof Instant) {
        return (Instant) temporal;
    }
    Objects.requireNonNull(temporal, "temporal");
    try {
        long instantSecs = temporal.getLong(INSTANT_SECONDS);
        int nanoOfSecond = temporal.get(NANO_OF_SECOND);
        return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName(), ex);
    }
}
 
Example 19
Source File: Instant.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code Instant} from a temporal object.
 * <p>
 * This obtains an instant based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code Instant}.
 * <p>
 * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code Instant::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the instant, not null
 * @throws DateTimeException if unable to convert to an {@code Instant}
 */
public static Instant from(TemporalAccessor temporal) {
    if (temporal instanceof Instant) {
        return (Instant) temporal;
    }
    Objects.requireNonNull(temporal, "temporal");
    try {
        long instantSecs = temporal.getLong(INSTANT_SECONDS);
        int nanoOfSecond = temporal.get(NANO_OF_SECOND);
        return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName(), ex);
    }
}
 
Example 20
Source File: Instant.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance of {@code Instant} from a temporal object.
 * <p>
 * This obtains an instant based on the specified temporal.
 * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 * which this factory converts to an instance of {@code Instant}.
 * <p>
 * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used as a query via method reference, {@code Instant::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the instant, not null
 * @throws DateTimeException if unable to convert to an {@code Instant}
 */
public static Instant from(TemporalAccessor temporal) {
    if (temporal instanceof Instant) {
        return (Instant) temporal;
    }
    Objects.requireNonNull(temporal, "temporal");
    try {
        long instantSecs = temporal.getLong(INSTANT_SECONDS);
        int nanoOfSecond = temporal.get(NANO_OF_SECOND);
        return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName(), ex);
    }
}