當前位置: 首頁>>代碼示例>>Java>>正文


Java ZoneId.getRules方法代碼示例

本文整理匯總了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
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:20,代碼來源:TCKZoneIdSerialization.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:ChronoZonedDateTimeImpl.java

示例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);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:36,代碼來源:ChronoZonedDateTimeImpl.java

示例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);
}
 
開發者ID:kiegroup,項目名稱:optashift-employee-rostering,代碼行數:40,代碼來源:ChronoZonedDateTimeImpl.java

示例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...
}
 
開發者ID:kiegroup,項目名稱:optashift-employee-rostering,代碼行數:15,代碼來源:Chrono.java


注:本文中的java.time.ZoneId.getRules方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。