本文整理汇总了Java中java.time.zone.ZoneRules.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java ZoneRules.getOffset方法的具体用法?Java ZoneRules.getOffset怎么用?Java ZoneRules.getOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.zone.ZoneRules
的用法示例。
在下文中一共展示了ZoneRules.getOffset方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的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);
}
示例2: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset time with the same instant as that specified. Finding the offset from
* UTC/Greenwich is simple as there is only one valid offset for each instant.
* <p>
* The date component of the instant is dropped during the conversion. This means that the conversion can
* never fail due to the instant being out of the valid range of dates.
*
* @param instant the instant to create the time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset time, not null
*/
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
Jdk7Methods.Objects_requireNonNull(instant, "instant");
Jdk7Methods.Objects_requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
long secsOfDay = instant.getEpochSecond() % SECONDS_PER_DAY;
secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
if (secsOfDay < 0) {
secsOfDay += SECONDS_PER_DAY;
}
LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, instant.getNano());
return new OffsetTime(time, offset);
}
示例3: 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);
}
示例4: create
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code ZonedDateTime} using seconds from the epoch of 1970-01-01T00:00:00Z.
*
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
* @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
* @param zone the time-zone, not null
* @return the zoned date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
ZoneRules rules = zone.getRules();
Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by
// epochSeconds
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
return new ZonedDateTime(ldt, offset, zone);
}
示例5: localInstant
import java.time.zone.ZoneRules; //导入方法依赖的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...
}
示例6: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset time with the same instant as that specified.
* Finding the offset from UTC/Greenwich is simple as there is only one valid
* offset for each instant.
* <p>
* The date component of the instant is dropped during the conversion.
* This means that the conversion can never fail due to the instant being
* out of the valid range of dates.
*
* @param instant the instant to create the time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset time, not null
*/
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
long localSecond = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later
int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
return new OffsetTime(time, offset);
}
示例7: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset time with the same instant as that specified.
* Finding the offset from UTC/Greenwich is simple as there is only one valid
* offset for each instant.
* <p>
* The date component of the instant is dropped during the conversion.
* This means that the conversion can never fail due to the instant being
* out of the valid range of dates.
*
* @param instant the instant to create the time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset time, not null
*/
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
long localSecond = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later
int secsOfDay = Math.floorMod(localSecond, SECONDS_PER_DAY);
LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
return new OffsetTime(time, offset);
}
示例8: normalized
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
* <p>
* The returns a normalized {@code ZoneId} that can be used in place of this ID.
* The result will have {@code ZoneRules} equivalent to those returned by this object,
* however the ID returned by {@code getId()} may be different.
* <p>
* The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
* If they do, then the {@code ZoneOffset} equal to that offset is returned.
* Otherwise {@code this} is returned.
*
* @return the time-zone unique ID, not null
*/
public ZoneId normalized() {
try {
ZoneRules rules = getRules();
if (rules.isFixedOffset()) {
return rules.getOffset(Instant.EPOCH);
}
} catch (ZoneRulesException ex) {
// invalid ZoneRegion is not important to this method
}
return this;
}
示例9: create
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code ZonedDateTime} using seconds from the
* epoch of 1970-01-01T00:00:00Z.
*
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
* @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
* @param zone the time-zone, not null
* @return the zoned date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
ZoneRules rules = zone.getRules();
Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by epochSeconds
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
return new ZonedDateTime(ldt, offset, zone);
}
示例10: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset date-time with the same instant as that specified. Finding the offset from
* UTC/Greenwich is simple as there is only one valid offset for each instant.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
Jdk7Methods.Objects_requireNonNull(instant, "instant");
Jdk7Methods.Objects_requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
return new OffsetDateTime(ldt, offset);
}
示例11: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
* <p>
* This creates an offset date-time with the same instant as that specified.
* Finding the offset from UTC/Greenwich is simple as there is only one valid
* offset for each instant.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the offset date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
return new OffsetDateTime(ldt, offset);
}
示例12: ofInstant
import java.time.zone.ZoneRules; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
* <p>
* This creates a local date-time based on the specified instant.
* First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
* which is simple as there is only one valid offset for each instant.
* Then, the instant and offset are used to calculate the local date-time.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, which may be an offset, not null
* @return the local date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
}