java.time.ZonedDateTime.ofLocal()方法的使用及代码示例

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

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

ZonedDateTime.ofLocal介绍

[英]Obtains an instance of ZonedDateTime from a local date-time using the preferred offset if possible.

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 ZoneRules of the zone ID.

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".

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".
[中]如果可能,使用首选偏移量从本地日期时间获取ZoneDateTime的实例。
本地日期时间解析为时间线上的一个瞬间。这是通过查找由分区ID的分区表定义的本地日期时间与UTC/格林威治的有效偏移量来实现的。
在大多数情况下,本地日期时间只有一个有效偏移量。在重叠的情况下,时钟被向后设置,有两个有效偏移。如果首选偏移是有效偏移之一,则使用它。否则,将使用之前的有效偏移量,通常对应于“summer”。
在间隙的情况下,时钟向前跳,没有有效的偏移。相反,本地日期时间会根据间隔的长度调整为更晚。对于典型的一小时夏令时更改,本地日期时间将在一小时后移动到通常对应于“夏季”的偏移量中。

代码示例

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

/**
 * Resolves the new local date-time using this zone ID, retaining the offset if possible.
 *
 * @param newDateTime  the new local date-time, not null
 * @return the zoned date-time, not null
 */
private ZonedDateTime resolveLocal(LocalDateTime newDateTime) {
  return ofLocal(newDateTime, zone, offset);
}

代码示例来源:origin: EvoSuite/evosuite

public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
  return ZonedDateTime.ofLocal(localDateTime, zone, preferredOffset);
}

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

/**
 * Combines this date-time with a time-zone to create a {@code ZonedDateTime}
 * trying to keep the same local date and time.
 * <p>
 * This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone.
 * Where possible, the result will have the same local date-time as this object.
 * <p>
 * Time-zone rules, such as daylight savings, mean that not every time on the
 * local time-line exists. If the local date-time is in a gap or overlap according to
 * the rules then a resolver is used to determine the resultant local time and offset.
 * This method uses {@link ZonedDateTime#ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
 * to retain the offset from this instance if possible.
 * <p>
 * Finer control over gaps and overlaps is available in two ways.
 * If you simply want to use the later offset at overlaps then call
 * {@link ZonedDateTime#withLaterOffsetAtOverlap()} immediately after this method.
 * <p>
 * To create a zoned date-time at the same instant irrespective of the local time-line,
 * use {@link #atZoneSameInstant(ZoneId)}.
 * To use the offset as the zone ID, use {@link #toZonedDateTime()}.
 *
 * @param zone  the time-zone to use, not null
 * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
 */
public ZonedDateTime atZoneSimilarLocal(ZoneId zone) {
  return ZonedDateTime.ofLocal(dateTime, zone, offset);
}

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

/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time.
 * <p>
 * This creates a zoned date-time matching the input local date-time as closely as possible.
 * Time-zone rules, such as daylight savings, mean that not every local date-time
 * is valid for the specified zone, thus the local date-time may be adjusted.
 * <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, when clocks are set back, there are two valid offsets.
 * This method uses the earlier offset typically corresponding to "summer".
 * <p>
 * In the case of a gap, when 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
 * @return the zoned date-time, not null
 */
public static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone) {
  return ofLocal(localDateTime, zone, null);
}

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

int hour, int minute, int second, int nanoOfSecond, ZoneId zone) {
LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
return ofLocal(dt, zone, null);

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

/**
 * Returns a copy of this date-time with a different time-zone,
 * retaining the local date-time if possible.
 * <p>
 * This method changes the time-zone and retains the local date-time.
 * The local date-time is only changed if it is invalid for the new zone,
 * determined using the same approach as
 * {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}.
 * <p>
 * To change the zone and adjust the local date-time,
 * use {@link #withZoneSameInstant(ZoneId)}.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param zone  the time-zone to change to, not null
 * @return a {@code ZonedDateTime} based on this date-time with the requested zone, not null
 */
@Override
public ZonedDateTime withZoneSameLocal(ZoneId zone) {
  Jdk8Methods.requireNonNull(zone, "zone");
  return this.zone.equals(zone) ? this : ofLocal(dateTime, zone, offset);
}

相关文章

ZonedDateTime类方法