本文整理汇总了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();
}
}
}
示例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
}
}
}
示例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
}
}
}
}
示例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
}
}
}
示例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);
}
}
示例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);
}
示例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);
}
}
示例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_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);
}
}
示例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);
}
}