org.threeten.bp.OffsetDateTime类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(401)

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

OffsetDateTime介绍

[英]A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-23T10:15:30+01:00.

OffsetDateTime is an immutable representation of a date-time with an offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00" can be stored in an OffsetDateTime.

OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds full time-zone rules.

It is intended that ZonedDateTime or Instant is used to model data in simpler applications. This class may be used when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol.

Specification for implementors

This class is immutable and thread-safe.
[中]在ISO-8601日历系统中,带有UTC/Greenwich偏移的日期时间,例如2007-12-23T10:15:30+01:00。
OffsetDateTime是带有偏移量的日期时间的不可变表示形式。此类存储所有日期和时间字段,精度为纳秒,以及UTC/格林威治的偏移量。例如,值“2007年10月2日13:45.30.123456789+02:00”可以存储在OffsetDateTime中。
OffsetDateTime、ZonedDateTime和Instant都将时间线上的一个瞬间存储为纳秒精度。瞬间是最简单的,只是代表瞬间。OffsetDateTime将UTC/Greenwich的偏移量添加到即时数据中,从而可以获取本地日期时间。ZoneDateTime添加了完整的时区规则。
ZoneDateTime或Instant用于在更简单的应用程序中建模数据。此类可用于更详细地建模日期时间概念,或与数据库或网络协议通信。
####实施者规范
这个类是不可变的,是线程安全的。

代码示例

代码示例来源:origin: ThreeTen/threetenbp

/**
 * Obtains an instance of {@code OffsetDateTime} from a text string
 * such as {@code 2007-12-23T10:15:30+01:00}.
 * <p>
 * The string must represent a valid date-time and is parsed using
 * {@link org.threeten.bp.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME}.
 *
 * @param text  the text to parse such as "2007-12-23T10:15:30+01:00", not null
 * @return the parsed offset date-time, not null
 * @throws DateTimeParseException if the text cannot be parsed
 */
public static OffsetDateTime parse(CharSequence text) {
  return parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

代码示例来源:origin: ThreeTen/threetenbp

@Override
  public OffsetDateTime queryFrom(TemporalAccessor temporal) {
    return OffsetDateTime.from(temporal);
  }
};

代码示例来源:origin: ThreeTen/threetenbp

/**
 * Combines this time with a date to create an {@code OffsetDateTime}.
 * <p>
 * This returns an {@code OffsetDateTime} formed from this time and the specified date.
 * All possible combinations of date and time are valid.
 *
 * @param date  the date to combine with, not null
 * @return the offset date-time formed from this time and the specified date, not null
 */
public OffsetDateTime atDate(LocalDate date) {
  return OffsetDateTime.of(date, time, offset);
}

代码示例来源:origin: com.uwetrottmann.trakt5/trakt-java

public static GsonBuilder getGsonBuilder() {
  GsonBuilder builder = new GsonBuilder();
  // trakt exclusively uses ISO 8601 date times with milliseconds and time zone offset
  // such as '2011-12-03T10:15:30.000+01:00' or '2011-12-03T10:15:30.000Z'
  builder.registerTypeAdapter(OffsetDateTime.class,
      (JsonDeserializer<OffsetDateTime>) (json, typeOfT, context) -> OffsetDateTime.parse(json.getAsString()));
  builder.registerTypeAdapter(OffsetDateTime.class,
      (JsonSerializer<OffsetDateTime>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString()));
  // dates are in ISO 8601 format as well
  builder.registerTypeAdapter(LocalDate.class,
      (JsonDeserializer<LocalDate>) (json, typeOfT, context) -> LocalDate.parse(json.getAsString()));
  // privacy
  builder.registerTypeAdapter(ListPrivacy.class,
      (JsonDeserializer<ListPrivacy>) (json, typeOfT, context) -> ListPrivacy.fromValue(json.getAsString()));
  // rating
  builder.registerTypeAdapter(Rating.class,
      (JsonDeserializer<Rating>) (json, typeOfT, context) -> Rating.fromValue(json.getAsInt()));
  builder.registerTypeAdapter(Rating.class,
      (JsonSerializer<Rating>) (src, typeOfSrc, context) -> new JsonPrimitive(src.value));
  // sort by
  builder.registerTypeAdapter(SortBy.class,
      (JsonDeserializer<SortBy>) (json, typeOfT, context) -> SortBy.fromValue(json.getAsString()));
  // sort how
  builder.registerTypeAdapter(SortHow.class,
      (JsonDeserializer<SortHow>) (json, typeOfT, context) -> SortHow.fromValue(json.getAsString()));
  // status
  builder.registerTypeAdapter(Status.class,
      (JsonDeserializer<Status>) (json, typeOfT, context) -> Status.fromValue(json.getAsString()));
  return builder;
}

代码示例来源:origin: ngs-doo/dsl-json

if (tmp[19] == '.') {
    final int nanos = readNanos(tmp, len - 1);
    return OffsetDateTime.of(year, month, day, hour, min, sec, nanos, ZoneOffset.UTC);
  return OffsetDateTime.of(year, month, day, hour, min, sec, 0, ZoneOffset.UTC);
} else if (len > 22 && len < 36 && tmp[len - 3] == ':'
    && (tmp[len - 6] == '+' || tmp[len - 6] == '-')
  if (tmp[19] == '.') {
    final int nanos = readNanos(tmp, len - 6);
    return OffsetDateTime.of(year, month, day, hour, min, sec, nanos, offset);
  return OffsetDateTime.of(year, month, day, hour, min, sec, 0, offset);
} else {
  return OffsetDateTime.parse(new String(tmp, 0, len));

代码示例来源:origin: alexvoronov/geonetworking

@Override
public LongPositionVector getLatestPosition() {
  Optional<Address> emptyAddress = Optional.empty();
  if (lastSeenTPV == null) {
    Instant timestamp = Instant.now();
    Position position = new Position(Double.NaN, Double.NaN);  // NaN or 0?
    boolean isPositionConfident = false;
    double speedMetersPerSecond = 0;
    double headingDegreesFromNorth = 0;
    return new LongPositionVector(emptyAddress, timestamp, position, isPositionConfident,
        speedMetersPerSecond, headingDegreesFromNorth);
  } else {
    final TPV tpv = lastSeenTPV;  // Is this enough to ensure that tpv will remain the same
                   // through the rest of the method?
    Instant timestamp = OffsetDateTime.parse(tpv.time()).toInstant();
    Position position = new Position(tpv.lat(), tpv.lon());
    boolean isPositionConfident = false;  // TODO: double-check conditions for PAI=true.
    double speedMetersPerSecond = tpv.speed();
    double headingDegreesFromNorth = tpv.track();
    return new LongPositionVector(emptyAddress, timestamp, position, isPositionConfident,
        speedMetersPerSecond, headingDegreesFromNorth);
  }
}

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

@Override
  public OffsetDateTime apply(OffsetDateTime d, ZoneId z) {
    return d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime()));
  }
},

代码示例来源:origin: ThreeTen/threetenbp

/**
 * Combines this instant with an offset to create an {@code OffsetDateTime}.
 * <p>
 * This returns an {@code OffsetDateTime} formed from this instant at the
 * specified offset from UTC/Greenwich. An exception will be thrown if the
 * instant is too large to fit into an offset date-time.
 * <p>
 * This method is equivalent to
 * {@link OffsetDateTime#ofInstant(Instant, ZoneId) OffsetDateTime.ofInstant(this, offset)}.
 *
 * @param offset  the offset to combine with, not null
 * @return the offset date-time formed from this instant and the specified offset, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public OffsetDateTime atOffset(ZoneOffset offset) {
  return OffsetDateTime.ofInstant(this, offset);
}

代码示例来源:origin: org.threeten/threetenbp

if (getOffset().equals(other.getOffset())) {
  return toLocalDateTime().compareTo(other.toLocalDateTime());
int cmp = Jdk8Methods.compareLongs(toEpochSecond(), other.toEpochSecond());
if (cmp == 0) {
  cmp = toLocalTime().getNano() - other.toLocalTime().getNano();
  if (cmp == 0) {
    cmp = toLocalDateTime().compareTo(other.toLocalDateTime());

代码示例来源:origin: ThreeTen/threetenbp

try {
  LocalDateTime ldt = LocalDateTime.from(temporal);
  return OffsetDateTime.of(ldt, offset);
} catch (DateTimeException ignore) {
  Instant instant = Instant.from(temporal);
  return OffsetDateTime.ofInstant(instant, offset);

代码示例来源:origin: ThreeTen/threetenbp

OffsetDateTime end = OffsetDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
  end = end.withOffsetSameInstant(offset);
  return dateTime.until(end.dateTime, unit);

代码示例来源:origin: org.threeten/threetenbp

ChronoField f = (ChronoField) field;
switch (f) {
  case INSTANT_SECONDS: return ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
  case OFFSET_SECONDS: {
    return with(dateTime, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
return with(dateTime.with(field, newValue), offset);

代码示例来源:origin: alexvoronov/geonetworking

/** Returns TAI milliseconds mod 2^32 for the given date.
 *
 * Since java int is signed 32 bit integer, return long instead.
 * It is the same on byte level, but just to avoid confusing people with negative values here.
 *
 *
 * From http://stjarnhimlen.se/comp/time.html:
 *
 * TAI (Temps Atomique International or International Atomic Time) is
 * defined as the weighted average of the time kept by about 200
 * atomic clocks in over 50 national laboratories worldwide.
 * TAI-UT1 was approximately 0 on 1958 Jan 1.
 * (TAI is ahead of UTC by 35 seconds as of 2014.)
 *
 * GPS time = TAI - 19 seconds.  GPS time matched UTC from 1980-01-01
 * to 1981-07-01.  No leap seconds are inserted into GPS time, thus
 * GPS time is 13 seconds ahead of UTC on 2000-01-01.  The GPS epoch
 * is 00:00 (midnight) UTC on 1980-01-06.
 * The difference between GPS Time and UTC changes in increments of
 * seconds each time a leap second is added to UTC time scale.
 */
public static long instantToTaiMillisSince2004Mod32(Instant instantX) {
  OffsetDateTime gnEpochStart =
      OffsetDateTime.of(LocalDateTime.of(2004, Month.JANUARY, 1, 0, 0), ZoneOffset.UTC);
  long millis2004 = gnEpochStart.toInstant().toEpochMilli();
  long millisAtX = instantX.toEpochMilli();
  long taiMillis = (millisAtX + LEAP_SECONDS_SINCE_2004*1000) - millis2004;
  return taiMillis % (1L << 32);
}

代码示例来源:origin: XeroAPI/Xero-Java

OffsetDateTime invModified =  OffsetDateTime.of(LocalDateTime.of(2018, 12, 06, 15, 00), ZoneOffset.UTC);
System.out.println(invModified.toString());

代码示例来源:origin: ThreeTen/threetenbp

return with(dateTime.with(adjuster), offset);
} else if (adjuster instanceof Instant) {
  return ofInstant((Instant) adjuster, offset);
} else if (adjuster instanceof ZoneOffset) {
  return with(dateTime, (ZoneOffset) adjuster);
} else if (adjuster instanceof OffsetDateTime) {
  return (OffsetDateTime) adjuster;

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

@Override
  public long applyAsLong(OffsetDateTime dt) {
    return dt.toInstant().toEpochMilli();
  }
},

代码示例来源:origin: gengstrand/clojure-news-feed

@Override
 public OffsetDateTime apply(OffsetDateTime d, ZoneId z) {
  return d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime()));
 }
}

代码示例来源:origin: org.threeten/threetenbp

/**
 * Combines this instant with an offset to create an {@code OffsetDateTime}.
 * <p>
 * This returns an {@code OffsetDateTime} formed from this instant at the
 * specified offset from UTC/Greenwich. An exception will be thrown if the
 * instant is too large to fit into an offset date-time.
 * <p>
 * This method is equivalent to
 * {@link OffsetDateTime#ofInstant(Instant, ZoneId) OffsetDateTime.ofInstant(this, offset)}.
 *
 * @param offset  the offset to combine with, not null
 * @return the offset date-time formed from this instant and the specified offset, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public OffsetDateTime atOffset(ZoneOffset offset) {
  return OffsetDateTime.ofInstant(this, offset);
}

代码示例来源:origin: ThreeTen/threetenbp

if (getOffset().equals(other.getOffset())) {
  return toLocalDateTime().compareTo(other.toLocalDateTime());
int cmp = Jdk8Methods.compareLongs(toEpochSecond(), other.toEpochSecond());
if (cmp == 0) {
  cmp = toLocalTime().getNano() - other.toLocalTime().getNano();
  if (cmp == 0) {
    cmp = toLocalDateTime().compareTo(other.toLocalDateTime());

代码示例来源:origin: org.threeten/threetenbp

try {
  LocalDateTime ldt = LocalDateTime.from(temporal);
  return OffsetDateTime.of(ldt, offset);
} catch (DateTimeException ignore) {
  Instant instant = Instant.from(temporal);
  return OffsetDateTime.ofInstant(instant, offset);

相关文章