Java Code Examples for org.threeten.bp.temporal.TemporalAccessor#getLong()

The following examples show how to use org.threeten.bp.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: DateTimeBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void crossCheck(TemporalAccessor temporal) {
    Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator();
    while (it.hasNext()) {
        Entry<TemporalField, Long> entry = it.next();
        TemporalField field = entry.getKey();
        long value = entry.getValue();
        if (temporal.isSupported(field)) {
            long temporalValue;
            try {
                temporalValue = temporal.getLong(field);
            } catch (RuntimeException ex) {
                continue;
            }
            if (temporalValue != value) {
                throw new DateTimeException("Cross check failed: " +
                        field + " " + temporalValue + " vs " + field + " " + value);
            }
            it.remove();
        }
    }
}
 
Example 2
Source File: TzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int parseSecs(String str) {
    if (str.equals("-")) {
        return 0;
    }
    int pos = 0;
    if (str.startsWith("-")) {
        pos = 1;
    }
    ParsePosition pp = new ParsePosition(pos);
    TemporalAccessor parsed = TIME_PARSER.parseUnresolved(str, pp);
    if (parsed == null || pp.getErrorIndex() >= 0) {
        throw new IllegalArgumentException(str);
    }
    long hour = parsed.getLong(HOUR_OF_DAY);
    Long min = (parsed.isSupported(MINUTE_OF_HOUR) ? parsed.getLong(MINUTE_OF_HOUR) : null);
    Long sec = (parsed.isSupported(SECOND_OF_MINUTE) ? parsed.getLong(SECOND_OF_MINUTE) : null);
    int secs = (int) (hour * 60 * 60 + (min != null ? min : 0) * 60 + (sec != null ? sec : 0));
    if (pos == 1) {
        secs = -secs;
    }
    return secs;
}
 
Example 3
Source File: AbstractDateTimeTest.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void basicTest_getLong_DateTimeField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example 4
Source File: AbstractDateTimeTest.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void basicTest_getLong_DateTimeField_null() {
    for (TemporalAccessor sample : samples()) {
        try {
            sample.getLong(null);
            fail("Failed on " + sample);
        } catch (NullPointerException ex) {
            // expected
        }
    }
}
 
Example 5
Source File: TestDateTimeFormatters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 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 6
Source File: Instant.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains an instance of {@code Instant} from a temporal object.
 * <p>
 * A {@code TemporalAccessor} represents some form of date and time information.
 * This factory converts the arbitrary temporal object 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) {
    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 + ", type " + temporal.getClass().getName(), ex);
    }
}