當前位置: 首頁>>代碼示例>>Java>>正文


Java TemporalAccessor.getLong方法代碼示例

本文整理匯總了Java中java.time.temporal.TemporalAccessor.getLong方法的典型用法代碼示例。如果您正苦於以下問題:Java TemporalAccessor.getLong方法的具體用法?Java TemporalAccessor.getLong怎麽用?Java TemporalAccessor.getLong使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.temporal.TemporalAccessor的用法示例。


在下文中一共展示了TemporalAccessor.getLong方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: crossCheck

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:21,代碼來源:Parsed.java

示例2: basicTest_getLong_TemporalField_supported

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@Test()
public void basicTest_getLong_TemporalField_supported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : validFields()) {
            sample.getLong(field);  // no exception
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:AbstractDateTimeTest.java

示例3: basicTest_getLong_TemporalField_unsupported

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@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
            }
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:14,代碼來源:AbstractDateTimeTest.java

示例4: basicTest_getLong_TemporalField_null

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@Test()
public void basicTest_getLong_TemporalField_null() {
    for (TemporalAccessor sample : samples()) {
        try {
            sample.getLong(null);
            fail("Failed on " + sample);
        } catch (NullPointerException ex) {
            // expected
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:AbstractDateTimeTest.java

示例5: test_parse_textField

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@Test(dataProvider="FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'e' :
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b
            = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        long value = parsed.getLong(field);
        assertEquals(value, expectedValue, "Value incorrect for " + field);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:34,代碼來源:TCKLocalizedFieldParser.java

示例6: assertParseMatch

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:TCKDateTimeFormatters.java

示例7: test_parse_textField

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@Test(dataProvider="FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'c' :
        case 'e' :
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b
            = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        long value = parsed.getLong(field);
        assertEquals(value, expectedValue, "Value incorrect for " + field);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:35,代碼來源:TCKLocalizedFieldParser.java

示例8: from

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
/**
 * 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 in queries 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);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:43,代碼來源:ZonedDateTime.java

示例9: from

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
/**
 * 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);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:43,代碼來源:ZonedDateTime.java

示例10: test_getLong_TemporalField_invalidField

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
@Test(expectedExceptions=DateTimeException.class)
public void test_getLong_TemporalField_invalidField() {
    for (TemporalAccessor sample : samples()) {
        sample.getLong(MockFieldNoValue.INSTANCE);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:7,代碼來源:AbstractDateTimeTest.java

示例11: from

import java.time.temporal.TemporalAccessor; //導入方法依賴的package包/類
/**
 * 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);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:32,代碼來源:Instant.java


注:本文中的java.time.temporal.TemporalAccessor.getLong方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。