您好有什么方法可以檢查給定的字符串是否是 epoch millisjava.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); }
3 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
任何正整數在理論上都是有效的。您可以根據自己對領域的了解添加約束。例如:未來沒有時間戳,因此所有大于 Instant.now().toEpochMillis() 的數字都是無效的。

慕田峪4524236
TA貢獻1875條經驗 獲得超5個贊
沒有,但這是該方法的實現:
public static boolean isEpochMillis(long epochMilli) { return true; }
當然,既然您看到了它的實現,您可能會發現這個方法是不必要的。

繁花不似錦
TA貢獻1851條經驗 獲得超4個贊
epochMilli 是自 1970-01-01 以來的毫秒數。所有長值都是有效的 epochMillis。歸結為您允許的日期。那將是你的范圍。
-2 將導致1969-12-31T23:59:59.998?Z。
添加回答
舉報
0/150
提交
取消