本文整理匯總了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
}
}
}
}
}
示例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));
}
示例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));
}
示例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));
}
示例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));
}
示例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
}
}
}
}
示例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
}
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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);
}
}