您好有什么方法可以檢查給定的字符串是否是 epoch millisjava.time 包中的以下代碼轉(zhuǎn)換為 EpochMillis。我正在尋找 isEpochMillis 以返回布爾條件。是否有來(lái)自 apache commons 或 google guava 的現(xiàn)成的 api 來(lái)檢查這個(gè)?就像 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); }
3 回答

有只小跳蛙
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
任何正整數(shù)在理論上都是有效的。您可以根據(jù)自己對(duì)領(lǐng)域的了解添加約束。例如:未來(lái)沒(méi)有時(shí)間戳,因此所有大于 Instant.now().toEpochMillis() 的數(shù)字都是無(wú)效的。

慕田峪4524236
TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
沒(méi)有,但這是該方法的實(shí)現(xiàn):
public static boolean isEpochMillis(long epochMilli) { return true; }
當(dāng)然,既然您看到了它的實(shí)現(xiàn),您可能會(huì)發(fā)現(xiàn)這個(gè)方法是不必要的。

繁花不似錦
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
epochMilli 是自 1970-01-01 以來(lái)的毫秒數(shù)。所有長(zhǎng)值都是有效的 epochMillis。歸結(jié)為您允許的日期。那將是你的范圍。
-2 將導(dǎo)致1969-12-31T23:59:59.998?Z。
添加回答
舉報(bào)
0/150
提交
取消