org.threeten.bp.Instant.getEpochSecond()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(151)

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

Instant.getEpochSecond介绍

[英]Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.

The epoch second count is a simple incrementing count of seconds where second 0 is 1970-01-01T00:00:00Z. The nanosecond part of the day is returned by getNanosOfSecond.
[中]获取Java纪元1970-01-01T00:00:00Z的秒数。
历元秒计数是秒的简单递增计数,其中秒0为1970-01-01T00:00:00Z。一天中的纳秒部分由getNanosOfSecond返回。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without
 * the timezone offset (always ends in "Z").
 */
public static Timestamp parseTimestamp(String timestamp) {
 Instant instant = Instant.parse(timestamp);
 return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano());
}

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

@Override
  public long applyAsLong(Instant value) {
    return value.getEpochSecond();
  }
},

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

@Override
public ZoneOffset getStandardOffset(Instant instant) {
  long epochSec = instant.getEpochSecond();
  int index  = Arrays.binarySearch(standardTransitions, epochSec);
  if (index < 0) {
    // switch negative insert position to start of matched range
    index = -index - 2;
  }
  return standardOffsets[index + 1];
}

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

@Override
public ZoneOffset getStandardOffset(Instant instant) {
  long epochSec = instant.getEpochSecond();
  int index  = Arrays.binarySearch(standardTransitions, epochSec);
  if (index < 0) {
    // switch negative insert position to start of matched range
    index = -index - 2;
  }
  return standardOffsets[index + 1];
}

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

/**
 * Converts an {@code Instant} to a {@code java.sql.Timestamp}.
 *
 * @param instant  the instant, not null
 * @return the SQL timestamp, not null
 */
public static Timestamp toSqlTimestamp(Instant instant) {
  try {
    Timestamp ts = new Timestamp(instant.getEpochSecond() * 1000);
    ts.setNanos(instant.getNano());
    return ts;
  } catch (ArithmeticException ex) {
    throw new IllegalArgumentException(ex);
  }
}

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

/**
 * Converts an {@code Instant} to a {@code java.sql.Timestamp}.
 *
 * @param instant  the instant, not null
 * @return the SQL timestamp, not null
 */
public static Timestamp toSqlTimestamp(Instant instant) {
  try {
    Timestamp ts = new Timestamp(instant.getEpochSecond() * 1000);
    ts.setNanos(instant.getNano());
    return ts;
  } catch (ArithmeticException ex) {
    throw new IllegalArgumentException(ex);
  }
}

代码示例来源:origin: com.google.cloud/google-cloud-core

/**
 * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without
 * the timezone offset (always ends in "Z").
 */
public static Timestamp parseTimestamp(String timestamp) {
 Instant instant = Instant.parse(timestamp);
 return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano());
}

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

/**
 * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}.
 * <p>
 * This creates a zoned date-time with the same instant as that specified.
 * Calling {@link #toInstant()} will return an instant equal to the one used here.
 * <p>
 * Converting an instant to a zoned date-time 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, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) {
  Jdk8Methods.requireNonNull(instant, "instant");
  Jdk8Methods.requireNonNull(zone, "zone");
  return create(instant.getEpochSecond(), instant.getNano(), zone);
}

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

/**
 * Obtains an instance of {@code ZonedDateTime} from an {@code Instant}.
 * <p>
 * This creates a zoned date-time with the same instant as that specified.
 * Calling {@link #toInstant()} will return an instant equal to the one used here.
 * <p>
 * Converting an instant to a zoned date-time 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, not null
 * @return the zoned date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) {
  Jdk8Methods.requireNonNull(instant, "instant");
  Jdk8Methods.requireNonNull(zone, "zone");
  return create(instant.getEpochSecond(), instant.getNano(), zone);
}

代码示例来源:origin: com.torodb.kvdocument/mongo-converter

@Override
public BsonValue visit(DateTimeValue value, Void arg) {
  Instant instant = value.getValue().toInstant(ZoneOffset.UTC);
  return new BsonTimestamp(
      UnsignedInteger.valueOf(instant.getEpochSecond()).intValue(),
      instant.getNano()
  );
}

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

/**
 * 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) {
  Jdk8Methods.requireNonNull(instant, "instant");
  Jdk8Methods.requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
}

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

/**
 * Obtains the current date-time from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date-time.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@link Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date-time, not null
 */
public static LocalDateTime now(Clock clock) {
  Jdk8Methods.requireNonNull(clock, "clock");
  final Instant now = clock.instant();  // called once
  ZoneOffset offset = clock.getZone().getRules().getOffset(now);
  return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
}

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

/**
 * Obtains the current date-time from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date-time.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@link Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date-time, not null
 */
public static LocalDateTime now(Clock clock) {
  Jdk8Methods.requireNonNull(clock, "clock");
  final Instant now = clock.instant();  // called once
  ZoneOffset offset = clock.getZone().getRules().getOffset(now);
  return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
}

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

/**
 * 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 <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  Jdk8Methods.requireNonNull(offset, "offset");  // protect against bad ZoneRules
  LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
  @SuppressWarnings("unchecked")
  ChronoLocalDateTimeImpl<R> cldt = (ChronoLocalDateTimeImpl<R>) chrono.localDateTime(ldt);
  return new ChronoZonedDateTimeImpl<R>(cldt, offset, zone);
}

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

/**
 * 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 <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  Jdk8Methods.requireNonNull(offset, "offset");  // protect against bad ZoneRules
  LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
  @SuppressWarnings("unchecked")
  ChronoLocalDateTimeImpl<R> cldt = (ChronoLocalDateTimeImpl<R>) chrono.localDateTime(ldt);
  return new ChronoZonedDateTimeImpl<R>(cldt, offset, zone);
}

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

/**
 * 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) {
  Jdk8Methods.requireNonNull(instant, "instant");
  Jdk8Methods.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);
}

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

/**
 * 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) {
  Jdk8Methods.requireNonNull(instant, "instant");
  Jdk8Methods.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);
}

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

/**
 * Obtains the current date from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date - today.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@link Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date, not null
 */
public static LocalDate now(Clock clock) {
  Jdk8Methods.requireNonNull(clock, "clock");
  final Instant now = clock.instant();  // called once
  ZoneOffset offset = clock.getZone().getRules().getOffset(now);
  long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
  long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY);
  return LocalDate.ofEpochDay(epochDay);
}

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

/**
 * Obtains the current date from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date - today.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@link Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date, not null
 */
public static LocalDate now(Clock clock) {
  Jdk8Methods.requireNonNull(clock, "clock");
  final Instant now = clock.instant();  // called once
  ZoneOffset offset = clock.getZone().getRules().getOffset(now);
  long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
  long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY);
  return LocalDate.ofEpochDay(epochDay);
}

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

/**
 * Obtains the current time from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current time.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@link Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current time, not null
 */
public static LocalTime now(Clock clock) {
  Jdk8Methods.requireNonNull(clock, "clock");
  // inline OffsetTime factory to avoid creating object and InstantProvider checks
  final Instant now = clock.instant();  // called once
  ZoneOffset offset = clock.getZone().getRules().getOffset(now);
  long secsOfDay = now.getEpochSecond() % SECONDS_PER_DAY;
  secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
  if (secsOfDay < 0) {
    secsOfDay += SECONDS_PER_DAY;
  }
  return LocalTime.ofSecondOfDay(secsOfDay, now.getNano());
}

相关文章