本文整理了Java中java.time.OffsetDateTime.atZoneSimilarLocal()
方法的一些代码示例,展示了OffsetDateTime.atZoneSimilarLocal()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OffsetDateTime.atZoneSimilarLocal()
方法的具体详情如下:
包路径:java.time.OffsetDateTime
类名称:OffsetDateTime
方法名:atZoneSimilarLocal
[英]Combines this date-time with a time-zone to create a ZonedDateTimetrying to keep the same local date and time.
This returns a 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.
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 ZonedDateTime#ofLocal(LocalDateTime,ZoneId,ZoneOffset)to retain the offset from this instance if possible.
Finer control over gaps and overlaps is available in two ways. If you simply want to use the later offset at overlaps then call ZonedDateTime#withLaterOffsetAtOverlap() immediately after this method.
To create a zoned date-time at the same instant irrespective of the local time-line, use #atZoneSameInstant(ZoneId). To use the offset as the zone ID, use #toZonedDateTime().
[中]将此日期时间与时区组合以创建ZonedDateTime,并尝试保持相同的本地日期和时间。
这将返回从该日期时间和指定时区形成的ZonedDateTime。如果可能,结果将与此对象具有相同的本地日期时间。
时区规则,如夏令时,意味着并非每个时间都存在于当地时间线上。根据规则,如果本地日期时间处于间隙或重叠中,则使用冲突解决程序确定结果本地时间和偏移量。此方法使用ZoneDateTime#of Local(LocalDateTime、ZoneId、ZoneOffset)尽可能保留与此实例的偏移量。
可以通过两种方式更好地控制间隙和重叠。如果只想在重叠处使用后一个偏移量,那么在该方法之后立即调用ZonedDateTime#WithLaterOffsetToVerlap()。
要在同一时刻创建分区日期时间,而不考虑本地时间线,请使用#atZoneSameInstant(ZoneId)。要使用偏移量作为分区ID,请使用#toZonedDateTime()。
代码示例来源:origin: com.impossibl.pgjdbc-ng/pgjdbc-ng
private static Object convertOutput(Context context, Type type, LocalDateTime dateTime, Class<?> targetClass, Calendar targetCalendar) throws ConversionException {
if (targetClass == LocalDateTime.class) {
return dateTime;
}
if (targetClass == LocalDate.class) {
return dateTime.toLocalDate();
}
if (targetClass == String.class) {
return context.getTimestampFormat().getPrinter().format(dateTime);
}
ZoneId targetZoneId = targetCalendar.getTimeZone().toZoneId();
ZonedDateTime zonedDateTime = dateTime.atOffset(ZoneOffset.UTC).atZoneSimilarLocal(targetZoneId);
if (targetClass == Time.class) {
return new Time(zonedDateTime.withYear(1970).withDayOfYear(1).toInstant().toEpochMilli());
}
if (targetClass == Date.class) {
return new Date(zonedDateTime.truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli());
}
if (targetClass == Timestamp.class) {
return Timestamp.from(zonedDateTime.toInstant());
}
throw new ConversionException(type, targetClass);
}
代码示例来源:origin: infiniteautomation/ma-core-public
/**
* @param zdt
* @return
*/
public Instant getInstant(ZonedDateTime zdt) {
ZonedDateTime offset = zdt.withHour(hour).withMinute(minute).withSecond(second).withNano((int)(millisecond * 1000000l));
LocalDateTime ldt = zdt.toLocalDateTime();
ldt = ldt.withHour(hour).withMinute(minute).withSecond(second).withNano((int)(millisecond * 1000000l));
if(!zdt.getZone().getRules().isValidOffset(ldt, zdt.getOffset())) {
//Within a gap of DST so OR after the transition
ZoneOffsetTransition transition = zdt.getZone().getRules().nextTransition(zdt.toInstant());
if(!ldt.isAfter(transition.getDateTimeAfter())){
//In a gap so we shift our time forward to the end of the gap.
offset = transition.getDateTimeAfter().atZone(zdt.getZone());
}else {
//After a gap so ensure we use the next zone offset
offset = ldt.atOffset(transition.getOffsetAfter()).atZoneSimilarLocal(transition.getOffsetAfter());
}
}
return offset.toInstant();
}
}
代码示例来源:origin: arnaudroger/SimpleFlatMapper
@Test
public void testObjectToOffsetTime() throws Exception {
ZoneId zoneId = ZoneId.systemDefault();
OffsetTime offsetTime = OffsetTime.now(zoneId);
testObjectToOffsetTime(null, null);
testObjectToOffsetTime(offsetTime, offsetTime);
testObjectToOffsetTime(offsetTime.atDate(LocalDate.now()), offsetTime);
testObjectToOffsetTime(offsetTime.atDate(LocalDate.now()).atZoneSimilarLocal(zoneId), offsetTime);
testObjectToOffsetTime(offsetTime.atDate(LocalDate.now()).toLocalDateTime(), offsetTime);
testObjectToOffsetTime(offsetTime.atDate(LocalDate.now()).toLocalTime(), offsetTime);
testObjectToOffsetTime(offsetTime.atDate(LocalDate.now()).toInstant(), offsetTime);
testObjectToOffsetTime(Date.from(offsetTime.atDate(LocalDate.now()).toInstant()), offsetTime.truncatedTo(ChronoUnit.MILLIS));
try {
testObjectToOffsetTime("a string", offsetTime);
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
内容来源于网络,如有侵权,请联系作者删除!