本文整理匯總了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...
}