java.time.zone.ZoneRules.getTransition()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(66)

本文整理了Java中java.time.zone.ZoneRules.getTransition()方法的一些代码示例,展示了ZoneRules.getTransition()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZoneRules.getTransition()方法的具体详情如下:
包路径:java.time.zone.ZoneRules
类名称:ZoneRules
方法名:getTransition

ZoneRules.getTransition介绍

[英]Gets the offset transition applicable at the specified local date-time in these rules.

The mapping from a local date-time to an offset is not straightforward. There are three cases:

  • Normal, with one valid offset. For the vast majority of the year, the normal case applies, where there is a single valid offset for the local date-time.
  • Gap, with zero valid offsets. This is when clocks jump forward typically due to the spring daylight savings change from "winter" to "summer". In a gap there are local date-time values with no valid offset.
  • Overlap, with two valid offsets. This is when clocks are set back typically due to the autumn daylight savings change from "summer" to "winter". In an overlap there are local date-time values with two valid offsets.

A transition is used to model the cases of a Gap or Overlap. The Normal case will return null.

There are various ways to handle the conversion from a LocalDateTime. One technique, using this method, would be:

ZoneOffsetTransition trans = rules.getTransition(localDT); 
if (trans == null) { 
// Gap or Overlap: determine what to do from transition 
} else { 
// Normal case: only one valid offset 
zoneOffset = rule.getOffset(localDT); 
}

[中]获取在这些规则中指定的本地日期时间适用的偏移量转换。
从本地日期时间到偏移量的映射并不简单。有三种情况:
*正常,有一个有效偏移。对于一年中的绝大多数时间,正常情况适用,当地日期和时间只有一个有效的偏移量。
*间隙,具有零有效偏移。这通常是由于春季夏令时从“冬季”变为“夏季”,时钟向前跳的时候。在间隙中,存在没有有效偏移的本地日期时间值。
*重叠,具有两个有效偏移。这是因为秋季夏令时从“夏季”变为“冬季”,时钟通常会被延迟。在重叠中,有两个有效偏移的本地日期时间值。
过渡用于模拟间隙或重叠的情况。正常情况下将返回null。
有多种方法可以处理LocalDateTime的转换。使用这种方法的一种技术是:

ZoneOffsetTransition trans = rules.getTransition(localDT); 
if (trans == null) { 
// Gap or Overlap: determine what to do from transition 
} else { 
// Normal case: only one valid offset 
zoneOffset = rule.getOffset(localDT); 
}

代码示例

代码示例来源:origin: com.addthis/cronus

/**
 * If there is no daylight savings time transition
 * the ignore any effects of daylight savings and return
 * the local output time.
 *
 * If there is a daylight savings time overlap transition
 * (the clocks are set back) then select the time zone
 * offset from before the transition.
 *
 * If there is a daylight saving time gap transition
 * (the clocks are set forward) then we cannot use the local
 * output time because it is not a legal time value.
 * Move the event to the end of the transition.
 */
private ZonedDateTime outputAdjustDaylightSavings(LocalDateTime output, ZoneId zoneId) {
  if (output == null) {
    return null;
  }
  ZoneRules zoneRules = zoneId.getRules();
  ZoneOffsetTransition transition = zoneRules.getTransition(output);
  if (transition == null) {
    return ZonedDateTime.of(output, zoneId);
  } else if (transition.isOverlap()) {
    return ZonedDateTime.ofInstant(output, transition.getOffsetBefore(), zoneId);
  } else {
    return ZonedDateTime.ofInstant(transition.getDateTimeAfter(), transition.getOffsetAfter(), zoneId);
  }
}

代码示例来源:origin: com.github.seratch/java-time-backport

offset = validOffsets.get(0);
} else if (validOffsets.size() == 0) {
  ZoneOffsetTransition trans = rules.getTransition(isoLDT);
  localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
  offset = trans.getOffsetAfter();

代码示例来源:origin: com.github.seratch/java-time-backport

offset = validOffsets.get(0);
} else if (validOffsets.size() == 0) {
  ZoneOffsetTransition trans = rules.getTransition(localDateTime);
  localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
  offset = trans.getOffsetAfter();

代码示例来源:origin: com.github.seratch/java-time-backport

if (zone instanceof ZoneOffset == false) {
  ZoneRules rules = zone.getRules();
  ZoneOffsetTransition trans = rules.getTransition(ldt);
  if (trans != null && trans.isGap()) {
    ldt = trans.getDateTimeAfter();

代码示例来源:origin: com.addthis/cronus

/**
 * If we are in the duplicated time period of an overlap transition,
 * then move forwards around the duplicated time period.
 */
private ZonedDateTime inputDaylightSavingsNext(ZonedDateTime input) {
  ZoneId zoneId = input.getZone();
  ZoneRules zoneRules = zoneId.getRules();
  ZoneOffset zoneOffset = input.getOffset();
  LocalDateTime localDateTime = input.toLocalDateTime();
  ZoneOffsetTransition transition = zoneRules.getTransition(localDateTime);
  if (transition == null) {
    return input;
  } else if (zoneOffset.equals(transition.getOffsetAfter())) {
    return ZonedDateTime.ofInstant(transition.getDateTimeBefore(),
                    transition.getOffsetAfter(), zoneId);
  } else {
    return input;
  }
}

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap() {
  ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
  if (trans != null && trans.isOverlap()) {
    ZoneOffset earlierOffset = trans.getOffsetBefore();
    if (earlierOffset.equals(offset) == false) {
      return new ChronoZonedDateTimeImpl<D>(dateTime, earlierOffset, zone);
    }
  }
  return this;
}

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
public ChronoZonedDateTime<D> withLaterOffsetAtOverlap() {
  ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
  if (trans != null) {
    ZoneOffset offset = trans.getOffsetAfter();
    if (offset.equals(getOffset()) == false) {
      return new ChronoZonedDateTimeImpl<D>(dateTime, offset, zone);
    }
  }
  return this;
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this date-time changing the zone offset to the
 * earlier of the two valid offsets at a local time-line overlap.
 * <p>
 * This method only has any effect when the local time-line overlaps, such as
 * at an autumn daylight savings cutover. In this scenario, there are two
 * valid offsets for the local date-time. Calling this method will return
 * a zoned date-time with the earlier of the two selected.
 * <p>
 * If this method is called when it is not an overlap, {@code this}
 * is returned.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @return a {@code ZonedDateTime} based on this date-time with the earlier offset, not null
 */
@Override
public ZonedDateTime withEarlierOffsetAtOverlap() {
  ZoneOffsetTransition trans = getZone().getRules().getTransition(dateTime);
  if (trans != null && trans.isOverlap()) {
    ZoneOffset earlierOffset = trans.getOffsetBefore();
    if (earlierOffset.equals(offset) == false) {
      return new ZonedDateTime(dateTime, earlierOffset, zone);
    }
  }
  return this;
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this date-time changing the zone offset to the
 * later of the two valid offsets at a local time-line overlap.
 * <p>
 * This method only has any effect when the local time-line overlaps, such as
 * at an autumn daylight savings cutover. In this scenario, there are two
 * valid offsets for the local date-time. Calling this method will return
 * a zoned date-time with the later of the two selected.
 * <p>
 * If this method is called when it is not an overlap, {@code this}
 * is returned.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null
 */
@Override
public ZonedDateTime withLaterOffsetAtOverlap() {
  ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime());
  if (trans != null) {
    ZoneOffset laterOffset = trans.getOffsetAfter();
    if (laterOffset.equals(offset) == false) {
      return new ZonedDateTime(dateTime, laterOffset, zone);
    }
  }
  return this;
}

代码示例来源:origin: com.addthis/cronus

localDateTime = localDateTime.minusMinutes(1);
ZoneOffsetTransition transition = zoneRules.getTransition(localDateTime);
if (transition == null) {
  return input;

代码示例来源:origin: com.github.seratch/java-time-backport

ZoneRules rules = zone.getRules();
if (rules.isValidOffset(localDateTime, offset) == false) {
  ZoneOffsetTransition trans = rules.getTransition(localDateTime);
  if (trans != null && trans.isGap()) {

相关文章