当前位置: 首页>>代码示例>>Java>>正文


Java TemporalAccessor.get方法代码示例

本文整理汇总了Java中java.time.temporal.TemporalAccessor.get方法的典型用法代码示例。如果您正苦于以下问题:Java TemporalAccessor.get方法的具体用法?Java TemporalAccessor.get怎么用?Java TemporalAccessor.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.temporal.TemporalAccessor的用法示例。


在下文中一共展示了TemporalAccessor.get方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: basicTest_get_TemporalField_supported

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test()
public void basicTest_get_TemporalField_supported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : validFields()) {
            if (sample.range(field).isIntValue()) {
                sample.get(field);  // no exception
            } else {
                try {
                    sample.get(field);
                    fail("Failed on " + sample + " " + field);
                } catch (DateTimeException ex) {
                    // expected
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:AbstractDateTimeTest.java

示例2: test_reducedWithLateChronoChange

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test
public void test_reducedWithLateChronoChange() {
    ThaiBuddhistDate date = ThaiBuddhistDate.of(2543, 1, 1);
    DateTimeFormatter df
            = new DateTimeFormatterBuilder()
                    .appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
                    .appendLiteral(" ")
                    .appendChronologyId()
            .toFormatter();
    int expected = date.get(YEAR);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely");
    assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            parsed.query(TemporalQueries.chronology()), input));

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:TestReducedParser.java

示例3: test_reducedWithChronoYear

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYear(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df
            = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
            .toFormatter()
            .withChronology(chrono);
    int expected = date.get(YEAR);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            chrono, input));

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:TestReducedParser.java

示例4: test_reducedWithChronoYearOfEra

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df
            = new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1))
            .toFormatter()
            .withChronology(chrono);
    int expected = date.get(YEAR_OF_ERA);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR_OF_ERA);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            chrono, input));

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:TestReducedParser.java

示例5: test_reducedWithLateChronoChangeTwice

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test
public void test_reducedWithLateChronoChangeTwice() {
    DateTimeFormatter df
            = new DateTimeFormatterBuilder()
                    .appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
                    .appendLiteral(" ")
                    .appendChronologyId()
                    .appendLiteral(" ")
                    .appendChronologyId()
            .toFormatter();
    int expected = 2044;
    String input = "44 ThaiBuddhist ISO";
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
    assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            parsed.query(TemporalQueries.chronology()), input));

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:23,代码来源:TestReducedParser.java

示例6: basicTest_get_TemporalField_unsupported

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test()
public void basicTest_get_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.get(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:AbstractDateTimeTest.java

示例7: basicTest_get_TemporalField_null

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test()
public void basicTest_get_TemporalField_null() {
    for (TemporalAccessor sample : samples()) {
        try {
            sample.get(null);
            fail("Failed on " + sample);
        } catch (NullPointerException ex) {
            // expected
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:AbstractDateTimeTest.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_get_TemporalField_invalidField

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void test_get_TemporalField_invalidField() {
    for (TemporalAccessor sample : samples()) {
        sample.get(MockFieldNoValue.INSTANCE);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:AbstractDateTimeTest.java

示例11: test

import java.time.temporal.TemporalAccessor; //导入方法依赖的package包/类
private String test(String fmtStr, Locale locale,
                           String expected, Object dt) {
    String out = new Formatter(
        new StringBuilder(), locale).format(fmtStr, dt).out().toString();
    if (verbose) {
        System.out.printf("%-24s  : %s%n", getClassName(dt), out);
    }

    // expected usually comes from Calendar which only has milliseconds
    // precision. So we're going to replace it's N:[nanos] stamp with
    // the correct value for nanos.
    if ((dt instanceof TemporalAccessor) && expected != null) {
        try {
            // Get millis & nanos from the dt
            final TemporalAccessor ta = (TemporalAccessor) dt;
            final int nanos = ta.get(ChronoField.NANO_OF_SECOND);
            final int millis = ta.get(ChronoField.MILLI_OF_SECOND);
            final String nanstr = String.valueOf(nanos);
            final String mistr = String.valueOf(millis);

            // Compute the value of the N:[nanos] field that we expect
            // to find in 'out'
            final StringBuilder sb = new StringBuilder();
            sb.append("N:[");
            for (int i=nanstr.length(); i<9; i++) {
                sb.append('0');
            }
            sb.append(nanos).append("]");

            // Compute the truncated value of N:[nanos] field that might
            // be in 'expected' when expected was built from Calendar.
            final StringBuilder sbm = new StringBuilder();
            sbm.append("N:[");
            for (int i=mistr.length(); i<3; i++) {
                sbm.append('0');
            }
            sbm.append(mistr).append("000000]");

            // if expected contains the truncated value, replace it with
            // the complete value.
            expected = expected.replace(sbm.toString(), sb.toString());
        } catch (UnsupportedTemporalTypeException e) {
            // nano seconds unsupported - nothing to do...
        }
    }
    if (expected != null && !out.equals(expected)) {
        System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
                          getClassName(dt), out, expected);
        new RuntimeException().printStackTrace(System.out);
        failure++;
    }
    total++;
    return out;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:55,代码来源:TestFormatter.java

示例12: 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.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。