0

嗨有没有办法检查给定的字符串是否是纪元毫秒

java.time 包中的以下代码转换为 EpochMillis。

我正在寻找 isEpochMillis 返回一个布尔条件。

有没有来自 apache commons 或 google guava 的现成 api 来检查这个?就像 isDouble isLong 等...

/**
     * Obtains an instance of {@code Instant} using milliseconds from the
     * epoch of 1970-01-01T00:00:00Z.
     * <p>
     * The seconds and nanoseconds are extracted from the specified milliseconds.
     *
     * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
     * @return an instant, not null
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
     */
    public static Instant ofEpochMilli(long epochMilli) {
        long secs = Math.floorDiv(epochMilli, 1000);
        int mos = (int)Math.floorMod(epochMilli, 1000);
        return create(secs, mos * 1000_000);
    }
4

3 回答 3

0

没有,但这里是该方法的一个实现:

public static boolean isEpochMillis(long epochMilli) { 
  return true; 
}

当然,你可能会发现这个方法是不必要的,现在你看到了它的实现。

于 2019-08-04T17:02:38.607 回答
0

正如 javadoc 所说,epochMilli 是自 1970-01-01 以来的毫秒数。所有长值都是有效的 epochMillis。它归结为您允许的日期。那将是你的范围。

-2 将导致1969-12-31T23:59:59.998 Z。

于 2019-07-31T16:08:28.853 回答
0

任何正整数在理论上都是有效的。您可以根据自己的领域知识添加约束。例如:未来没有时间戳,因此所有大于 Instant.now().toEpochMillis() 的数字都是无效的。

于 2019-07-31T16:07:57.673 回答