java.time.format.DateTimeFormatter.parsedLeapSecond()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(167)

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

DateTimeFormatter.parsedLeapSecond介绍

[英]A query that provides access to whether a leap-second was parsed.

This returns a singleton TemporalQuery that provides access to additional information from the parse. The query always returns a non-null boolean, true if parsing saw a leap-second, false if not.

Instant parsing handles the special "leap second" time of '23:59:60'. Leap seconds occur at '23:59:60' in the UTC time-zone, but at other local times in different time-zones. To avoid this potential ambiguity, the handling of leap-seconds is limited to DateTimeFormatterBuilder#appendInstant(), as that method always parses the instant with the UTC zone offset.

If the time '23:59:60' is received, then a simple conversion is applied, replacing the second-of-minute of 60 with 59. This query can be used on the parse result to determine if the leap-second adjustment was made. The query will return one second of excess if it did adjust to remove the leap-second, and zero if not. Note that applying a leap-second smoothing mechanism, such as UTC-SLS, is the responsibility of the application, as follows:

TemporalAccessor parsed = formatter.parse(str); 
Instant instant = parsed.query(Instant::from); 
if (parsed.query(DateTimeFormatter.parsedLeapSecond())) { 
// validate leap-second is correct and apply correct smoothing 
}

[中]提供对闰秒是否被解析的访问的查询。
这将返回一个单例临时查询,该查询提供对来自解析的附加信息的访问。查询总是返回一个非空布尔值,如果解析看到闰秒,则返回true,否则返回false。
即时解析处理特殊的“闰秒”时间“23:59:60”。闰秒发生在UTC时区的“23:59:60”,但在不同时区的其他本地时间。为了避免这种潜在的歧义,闰秒的处理仅限于DateTimeFormatterBuilder#appendInstant(),因为该方法始终使用UTC区域偏移量解析该瞬间。
如果接收到时间“23:59:60”,则应用简单转换,将60的秒数替换为59。此查询可用于分析结果,以确定是否进行了闰秒调整。如果查询调整以删除闰秒,则返回1秒的多余值,否则返回零。请注意,应用闰秒平滑机制(如UTC-SLS)是应用程序的责任,如下所示:

TemporalAccessor parsed = formatter.parse(str); 
Instant instant = parsed.query(Instant::from); 
if (parsed.query(DateTimeFormatter.parsedLeapSecond())) { 
// validate leap-second is correct and apply correct smoothing 
}

代码示例

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

/**
 * Obtains an instance of {@code UtcInstant} from a text string
 * {@code 2007-12-03T10:15:30.00Z}.
 * <p>
 * The string must represent a valid instant in UTC and is parsed using
 * {@link DateTimeFormatter#ISO_INSTANT} with leap seconds handled.
 *
 * @param text  the text to parse such as "12345.123456789s(TAI)", not null
 * @return the parsed instant, not null
 * @throws DateTimeParseException if the text cannot be parsed
 * @throws DateTimeException if parsed text represents an invalid leap second
 */
@FromString
public static UtcInstant parse(CharSequence text) {
  TemporalAccessor parsed = DateTimeFormatter.ISO_INSTANT.parse(text);
  long epochSecond = parsed.getLong(INSTANT_SECONDS);
  long nanoOfSecond = parsed.getLong(NANO_OF_SECOND);
  boolean leap = parsed.query(DateTimeFormatter.parsedLeapSecond());
  long epochDay = Math.floorDiv(epochSecond, SECS_PER_DAY);
  long mjd = epochDay + OFFSET_MJD_EPOCH;
  long nanoOfDay = Math.floorMod(epochSecond, SECS_PER_DAY) * NANOS_PER_SECOND + nanoOfSecond;
  if (leap) {
    nanoOfDay += NANOS_PER_SECOND;
  }
  return UtcInstant.ofModifiedJulianDay(mjd, nanoOfDay);
}

代码示例来源:origin: net.time4j/time4j-core

if (threeten.query(DateTimeFormatter.parsedLeapSecond())) {
  moment = moment.plus(1, SI.SECONDS);
  if (!moment.isLeapSecond()) {

相关文章