本文整理汇总了Java中java.time.ZoneId.from方法的典型用法代码示例。如果您正苦于以下问题:Java ZoneId.from方法的具体用法?Java ZoneId.from怎么用?Java ZoneId.from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.ZoneId
的用法示例。
在下文中一共展示了ZoneId.from方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: zonedDateTime
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Creates a zoned date-time in this chronology from another date-time object.
* <p>
* This creates a date-time in this chronology based on the specified {@code DateTimeAccessor}.
* <p>
* This should obtain a {@code ZoneId} using {@link ZoneId#from(DateTimeAccessor)}. The date-time should be
* obtained by obtaining an {@code Instant}. If that fails, the local date-time should be used.
*
* @param dateTime the date-time object to convert, not null
* @return the zoned date-time in this chronology, not null
* @throws DateTimeException if unable to create the date-time
*/
public ChronoZonedDateTime<C> zonedDateTime(DateTimeAccessor dateTime) {
try {
ZoneId zoneId = ZoneId.from(dateTime);
ChronoDateTimeImpl<C> cldt;
try {
Instant instant = Instant.from(dateTime);
cldt = localInstant(instant, zoneId);
} catch (DateTimeException ex1) {
cldt = ensureChronoLocalDateTime(localDateTime(dateTime));
}
return ChronoZonedDateTimeImpl.ofBest(cldt, zoneId, null);
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to convert DateTimeAccessor to ZonedDateTime: " + dateTime.getClass(), ex);
}
}
示例2: zonedDateTime
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
* <p>
* This obtains a zoned date-time in this chronology 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 ChronoZonedDateTime}.
* <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 ChronoLocalDateTime} if necessary.
* The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
* with {@code Instant} or {@code ChronoLocalDateTime}.
* Implementations are permitted to perform optimizations such as accessing
* those fields that are equivalent to the relevant objects.
* The result uses this chronology.
* <p>
* This method matches the signature of the functional interface {@link TemporalQuery}
* allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
*
* @param temporal the temporal object to convert, not null
* @return the zoned date-time in this chronology, not null
* @throws DateTimeException if unable to create the date-time
* @see ChronoZonedDateTime#from(TemporalAccessor)
*/
default ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal) {
try {
ZoneId zone = ZoneId.from(temporal);
try {
Instant instant = Instant.from(temporal);
return zonedDateTime(instant, zone);
} catch (DateTimeException ex1) {
ChronoLocalDateTimeImpl<?> cldt = ChronoLocalDateTimeImpl.ensureValid(this, localDateTime(temporal));
return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
}
} catch (DateTimeException ex) {
throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
}
}
示例3: factory_from_TemporalAccessor_invalid_noDerive
import java.time.ZoneId; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
ZoneId.from(LocalTime.of(12, 30));
}
示例4: factory_from_TemporalAccessor_null
import java.time.ZoneId; //导入方法依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
ZoneId.from(null);
}