本文整理了Java中java.time.OffsetDateTime.atZoneSameInstant()
方法的一些代码示例,展示了OffsetDateTime.atZoneSameInstant()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OffsetDateTime.atZoneSameInstant()
方法的具体详情如下:
包路径:java.time.OffsetDateTime
类名称:OffsetDateTime
方法名:atZoneSameInstant
[英]Combines this date-time with a time-zone to create a ZonedDateTimeensuring that the result has the same instant.
This returns a ZonedDateTime formed from this date-time and the specified time-zone. This conversion will ignore the visible local date-time and use the underlying instant instead. This avoids any problems with local time-line gaps or overlaps. The result might have different values for fields such as hour, minute an even day.
To attempt to retain the values of the fields, use #atZoneSimilarLocal(ZoneId). To use the offset as the zone ID, use #toZonedDateTime().
[中]将此日期时间与时区组合,以创建ZonedDateTime,确保结果具有相同的瞬间。
这将返回从该日期时间和指定时区形成的ZonedDateTime。此转换将忽略可见的本地日期时间,而是使用基础即时。这避免了任何与当地时间线间隔或重叠有关的问题。结果可能会有不同的字段值,例如小时、分钟甚至一天。
要尝试保留字段的值,请使用#atZoneSimilarLocal(ZoneId)。要使用偏移量作为分区ID,请使用#toZonedDateTime()。
代码示例来源:origin: google/data-transfer-project
private void copyDateTime(
String key,
CalendarEventModel.CalendarEventTime dateTime,
Map<String, Object> graphCalendar) {
Map<String, String> graphDateTime = new HashMap<>();
graphDateTime.put(
"dateTime",
dateTime.getDateTime().atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime().toString());
graphDateTime.put("timeZone", "UTC");
graphCalendar.put(key, graphDateTime);
}
代码示例来源:origin: com.thoughtworks.xstream/xstream
try {
final OffsetDateTime odt = OffsetDateTime.parse(str);
return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(odt.atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
return GregorianCalendar.from(ot.atDate(LocalDate.ofEpochDay(0)).atZoneSameInstant(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
return GregorianCalendar.from(ot.atDate(y.atMonthDay(md)).atZoneSameInstant(ZoneId.systemDefault()));
代码示例来源:origin: stackoverflow.com
OffsetDateTime createdOn = requestDto.getCreatedOn();
OffsetDateTime utc = createdOn.atZoneSameInstant(ZoneId.of("UTC"));
代码示例来源:origin: com.github.feature-flip/flips-core
private ZonedDateTime getCutoffDateTime(String datetime){
logger.info("DateTimeFlipCondition: parsing {}", datetime);
try{
return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC);
}
catch (DateTimeParseException e){
logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ");
throw e;
}
}
}
代码示例来源:origin: Feature-Flip/flips
private ZonedDateTime getCutoffDateTime(String datetime){
logger.info("DateTimeFlipCondition: parsing {}", datetime);
try{
return OffsetDateTime.parse(datetime).atZoneSameInstant(DateTimeUtils.UTC);
}
catch (DateTimeParseException e){
logger.error("Could not parse " + datetime + ", expected format yyyy-MM-ddTHH:mm:ssZ");
throw e;
}
}
}
代码示例来源:origin: marschall/threeten-jpa
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
OffsetDateTime dateTime = rs.getObject(names[TIMESTAMP_UTC_INDEX], OffsetDateTime.class);
String zoneIdName = rs.getString(names[ZONE_ID_INDEX]);
if (dateTime == null || zoneIdName == null) {
return null;
}
ZoneId zoneId;
try {
zoneId = ZoneId.of(zoneIdName);
} catch (DateTimeException e) {
throw new HibernateException("invalid zone id: " + zoneIdName, e);
}
return dateTime.atZoneSameInstant(zoneId);
}
代码示例来源:origin: org.graylog2.repackaged/grok
@Override
public Instant convert(String value) {
TemporalAccessor dt = formatter.parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, Instant::from, LocalDate::from);
if (dt instanceof ZonedDateTime) {
return ((ZonedDateTime)dt).toInstant();
} else if (dt instanceof LocalDateTime) {
return ((LocalDateTime) dt).atZone(timeZone).toInstant();
} else if (dt instanceof OffsetDateTime) {
return ((OffsetDateTime) dt).atZoneSameInstant(timeZone).toInstant();
} else if (dt instanceof Instant) {
return ((Instant) dt);
} else if (dt instanceof LocalDate) {
return ((LocalDate) dt).atStartOfDay(timeZone).toInstant();
} else {
return null;
}
}
代码示例来源:origin: goldmansachs/jdmn
private Duration toDuration(OffsetTime first, OffsetTime second) {
ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1);
return datatypeFactory.newDuration(durationInMilliSeconds);
}
}
代码示例来源:origin: com.goldmansachs.jdmn/jdmn-core
private Duration toDuration(OffsetTime first, OffsetTime second) {
ZonedDateTime first1 = first.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
ZonedDateTime second1 = second.atDate(DateTimeUtil.EPOCH).atZoneSameInstant(DateTimeUtil.UTC);
long durationInMilliSeconds = getDurationInMilliSeconds(first1, second1);
return datatypeFactory.newDuration(durationInMilliSeconds);
}
}
代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions
public static ZonedDateTime parse(Clock c, String str) {
if (PATTERN_DATETIME.matcher(str).matches()) return LocalDateTime.parse(str).atZone(c.getZone());
if (PATTERN_DATE.matcher(str).matches()) return LocalDate.parse(str).atStartOfDay(c.getZone());
if (PATTERN_MONTH.matcher(str).matches()) return LocalDate.parse(str + "-01").atStartOfDay(c.getZone());
return OffsetDateTime.parse(str).atZoneSameInstant(c.getZone());
}
代码示例来源:origin: no.api.meteo/meteo-core
public static ZonedDateTime fullOffsetFormatToZonedDateTime(String dateStr) throws MeteoException {
if (dateStr == null) {
return null;
}
String d = dateStr;
// Fix for invalid zone ids in the MET api
if (d.charAt(19) == '+') {
d = dateStr.substring(0, 22) + ":" + dateStr.substring(22);
}
try {
return OffsetDateTime.parse(d).atZoneSameInstant(ZoneId.of("Z"));
} catch (DateTimeParseException e) {
throw new MeteoException(e);
}
}
public static ZonedDateTime fullFormatToZonedDateTime(String dateStr) throws MeteoException {
代码示例来源:origin: com.haulmont.cuba/cuba-global
@Override
public String format(@Nullable Object value, Locale locale, TimeZone timeZone) {
if (timeZone == null || value == null) {
return format(value, locale);
}
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
LocalDateTime localDateTime = offsetDateTime.atZoneSameInstant(timeZone.toZoneId()).toLocalDateTime();
return format(localDateTime, locale);
}
代码示例来源:origin: Silverpeas/Silverpeas-Core
/**
* Gets optionally an event occurrence from the specified data.
* @param event an event.
* @param occurrenceStartDate a start date.
* @return the computed occurrence identifier.
*/
public static Optional<CalendarEventOccurrence> getBy(CalendarEvent event, Temporal occurrenceStartDate) {
Temporal startDate = occurrenceStartDate;
if (startDate instanceof OffsetDateTime) {
startDate = ((OffsetDateTime) occurrenceStartDate).atZoneSameInstant(ZoneOffset.UTC)
.toOffsetDateTime();
}
return getById(generateId(event, startDate));
}
代码示例来源:origin: org.jadira.usertype/usertype.extended
@Override
public OffsetDateTime fromNonNullValue(Timestamp value) {
ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneId() : getDatabaseZone();
ZoneId currentJavaZone = javaZone == null ? getDefaultZoneId() : javaZone;
OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone);
ZonedDateTime zonedDateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone);
dateTime = zonedDateTime.toOffsetDateTime();
return dateTime;
}
代码示例来源:origin: org.jadira.usertype/usertype.extended
@Override
public OffsetTime fromNonNullValue(Timestamp value) {
ZoneId currentDatabaseZone = getDatabaseZone() == null ? getDefaultZoneOffset() : getDatabaseZone();
ZoneId currentJavaZone = javaZone == null ? getDefaultZoneOffset() : javaZone;
OffsetDateTime dateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value.getTime()), currentDatabaseZone);
dateTime = dateTime.with(ChronoField.NANO_OF_SECOND, value.getNanos()).atZoneSameInstant(currentJavaZone).toOffsetDateTime();
OffsetTime time = dateTime.toOffsetTime();
return time;
}
代码示例来源:origin: org.hibernate.orm/hibernate-core
public Calendar fromString(String string) {
final OffsetTime parsedOffsetTime = OffsetTime.parse( string, FORMATTER );
return GregorianCalendar.from( parsedOffsetTime.atDate( LocalDate.MIN ).atZoneSameInstant( parsedOffsetTime.getOffset() ) );
}
代码示例来源:origin: Silverpeas/Silverpeas-Core
/**
* Triggers this reminder at the specified date time. The timezone of the specified date time
* will be set in the timezone of the user behind this reminder.
* @param dateTime the date time at which this reminder will be triggered once scheduled.
* @return itself.
*/
public DateTimeReminder triggerAt(final OffsetDateTime dateTime) {
final ZoneId userZoneId = User.getById(getUserId()).getUserPreferences().getZoneId();
this.dateTime = dateTime.atZoneSameInstant(userZoneId).toOffsetDateTime();
return this;
}
代码示例来源:origin: marschall/threeten-jpa
/**
* Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}.
*
* @param attribute the value to be converted, possibly {@code null}
* @return the converted data, possibly {@code null}
*/
public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) {
if (attribute == null) {
return null;
}
byte[] bytes = newTimestamptzBuffer();
ZonedDateTime utc = attribute.atZoneSameInstant(UTC);
writeDateTime(bytes, utc.toLocalDateTime());
ZoneOffset offset = attribute.getOffset();
writeZoneOffset(bytes, offset);
return new TIMESTAMPTZ(bytes);
}
代码示例来源:origin: com.github.marschall/threeten-jpa-oracle-impl
/**
* Converts {@link OffsetDateTime} to {@link TIMESTAMPTZ}.
*
* @param attribute the value to be converted, possibly {@code null}
* @return the converted data, possibly {@code null}
*/
public static TIMESTAMPTZ offsetDateTimeToTimestamptz(OffsetDateTime attribute) {
if (attribute == null) {
return null;
}
byte[] bytes = newTimestamptzBuffer();
ZonedDateTime utc = attribute.atZoneSameInstant(UTC);
writeDateTime(bytes, utc.toLocalDateTime());
ZoneOffset offset = attribute.getOffset();
writeZoneOffset(bytes, offset);
return new TIMESTAMPTZ(bytes);
}
代码示例来源:origin: arnaudroger/SimpleFlatMapper
@Test
public void testObjectToOffsetDateTime() throws Exception {
ZoneId zoneId = ZoneId.systemDefault();
OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneId);
testObjectToOffsetDateTime(null, null);
testObjectToOffsetDateTime(offsetDateTime, offsetDateTime);
testObjectToOffsetDateTime(offsetDateTime.toLocalDateTime(), offsetDateTime);
testObjectToOffsetDateTime(offsetDateTime.toInstant(), offsetDateTime);
testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
testObjectToOffsetDateTime(offsetDateTime.atZoneSameInstant(zoneId), offsetDateTime);
testObjectToOffsetDateTime(offsetDateTime.toLocalDate(), offsetDateTime.truncatedTo(ChronoUnit.DAYS));
testObjectToOffsetDateTime(Date.from(offsetDateTime.toInstant()), offsetDateTime.truncatedTo(ChronoUnit.MILLIS));
try {
testObjectToOffsetDateTime("a string", offsetDateTime);
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
内容来源于网络,如有侵权,请联系作者删除!