本文整理汇总了Java中java.time.ZoneId.getRules方法的典型用法代码示例。如果您正苦于以下问题:Java ZoneId.getRules方法的具体用法?Java ZoneId.getRules怎么用?Java ZoneId.getRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.ZoneId
的用法示例。
在下文中一共展示了ZoneId.getRules方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_deserialization_lenient_characters
import java.time.ZoneId; //导入方法依赖的package包/类
@Test
public void test_deserialization_lenient_characters() throws Exception {
// an ID can be loaded without validation during deserialization
String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
ZoneId deser = deserialize(id);
// getId, equals, hashCode, toString and normalized are OK
assertEquals(deser.getId(), id);
assertEquals(deser.toString(), id);
assertEquals(deser, deser);
assertEquals(deser.hashCode(), deser.hashCode());
assertEquals(deser.normalized(), deser);
// getting the rules is not
try {
deser.getRules();
fail();
} catch (ZoneRulesException ex) {
// expected
}
}
示例2: ofInstant
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Obtains an instance from an instant using the specified time-zone.
*
* @param chrono the chronology, not null
* @param instant the instant, not null
* @param zone the zone identifier, not null
* @return the zoned date-time, not null
*/
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
Objects.requireNonNull(offset, "offset"); // protect against bad ZoneRules
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
示例3: ofBest
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Obtains an instance from a local date-time using the preferred offset if possible.
*
* @param localDateTime the local date-time, not null
* @param zone the zone identifier, not null
* @param preferredOffset the zone offset, null if no preference
* @return the zoned date-time, not null
*/
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
Objects.requireNonNull(localDateTime, "localDateTime");
Objects.requireNonNull(zone, "zone");
if (zone instanceof ZoneOffset) {
return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
}
ZoneRules rules = zone.getRules();
LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
ZoneOffset offset;
if (validOffsets.size() == 1) {
offset = validOffsets.get(0);
} else if (validOffsets.size() == 0) {
ZoneOffsetTransition trans = rules.getTransition(isoLDT);
localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
offset = trans.getOffsetAfter();
} else {
if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
offset = preferredOffset;
} else {
offset = validOffsets.get(0);
}
}
Objects.requireNonNull(offset, "offset"); // protect against bad ZoneRules
return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
示例4: ofBest
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code ZonedDateTime} from a local date-time using the preferred offset if
* possible.
*
* @param localDateTime the local date-time, not null
* @param zoneId the zone identifier, not null
* @param preferredOffset the zone offset, null if no preference
* @return the zoned date-time, not null
*/
static <R extends Chrono<R>> ChronoZonedDateTime<R> ofBest(ChronoDateTimeImpl<R> localDateTime, ZoneId zoneId,
ZoneOffset preferredOffset) {
Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId");
if (zoneId instanceof ZoneOffset) {
return new ChronoZonedDateTimeImpl<R>(localDateTime, (ZoneOffset) zoneId, zoneId);
}
ZoneRules rules = zoneId.getRules();
LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
ZoneOffset offset;
if (validOffsets.size() == 1) {
offset = validOffsets.get(0);
} else if (validOffsets.size() == 0) {
// TODO what to do with chrono support in GWT?
// ZoneOffsetTransition trans = rules.getTransition(isoLDT);
// localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
// offset = trans.getOffsetAfter();
offset = rules.getOffset(isoLDT);
} else {
if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
offset = preferredOffset;
} else {
offset = validOffsets.get(0);
}
}
Jdk7Methods.Objects_requireNonNull(offset, "offset"); // protect against bad ZoneRules
return new ChronoZonedDateTimeImpl<R>(localDateTime, offset, zoneId);
}
示例5: localInstant
import java.time.ZoneId; //导入方法依赖的package包/类
/**
* Creates a local date-time in this chronology from an instant and zone.
*
* @param instant the instant, not null
* @param zoneId the zone ID, not null
* @return the local date-time, not null
*/
ChronoDateTimeImpl<C> localInstant(Instant instant, ZoneId zoneId) {
ZoneRules rules = zoneId.getRules();
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
return ChronoDateTimeImpl.of(dateNow(), LocalTime.MIDNIGHT).with(ldt); // not very efficient...
}