当前位置: 首页>>代码示例>>Java>>正文


Java ZoneRules.getValidOffsets方法代码示例

本文整理汇总了Java中java.time.zone.ZoneRules.getValidOffsets方法的典型用法代码示例。如果您正苦于以下问题:Java ZoneRules.getValidOffsets方法的具体用法?Java ZoneRules.getValidOffsets怎么用?Java ZoneRules.getValidOffsets使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.zone.ZoneRules的用法示例。


在下文中一共展示了ZoneRules.getValidOffsets方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ofLocal

import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:48,代码来源:ZonedDateTime.java

示例2: checkOffset

import java.time.zone.ZoneRules; //导入方法依赖的package包/类
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:17,代码来源:TCKZoneRules.java

示例3: ofBest

import java.time.zone.ZoneRules; //导入方法依赖的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: ofLocal

import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time using the preferred offset if
 * possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid
 * offset from UTC/Greenwich for the local date-time as defined by the {@link ZoneRules rules} of the zone
 * ID.
 * <p>
 * In most cases, there is only one valid offset for a local date-time. In the case of an overlap, where
 * clocks are set back, there are two valid offsets. If the preferred offset is one of the valid offsets
 * then it is used. Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset. Instead, the local date-time
 * is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the
 * local date-time will be moved one hour later into the offset typically corresponding to "summer".
 * 
 * @param localDateTime the local date-time, not null
 * @param zone the time-zone, not null
 * @param preferredOffset the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  if (zone instanceof ZoneOffset) {
    return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
  }
  ZoneRules rules = zone.getRules();
  List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
  ZoneOffset offset;
  if (validOffsets.size() == 1) {
    offset = validOffsets.get(0);
  } else if (validOffsets.size() == 0) {
    // ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    // localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
    offset = rules.getOffset(localDateTime); // trans.getOffsetAfter();
  } else {
    if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
      offset = preferredOffset;
    } else {
      offset = Jdk7Methods.Objects_requireNonNull(validOffsets.get(0), "offset"); // protect against bad
                                                                                  // ZoneRules
    }
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
 
开发者ID:kiegroup,项目名称:optashift-employee-rostering,代码行数:48,代码来源:ZonedDateTime.java

示例5: ofBest

import java.time.zone.ZoneRules; //导入方法依赖的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


注:本文中的java.time.zone.ZoneRules.getValidOffsets方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。