如何在java中验证unix时间戳?

问题描述 投票:0回答:3

我需要以毫秒为单位验证给定的输入字符串是否有效。


例如,如果给定

Timestamp


Timestamp

那么它应该返回true。

如果

String time ="1310966356458";

那么它应该返回 false;

请帮忙。预先感谢

java unix-timestamp
3个回答
13
投票

String time ="1000";

但是,时间戳也可能代表某人的出生时间。在这种情况下,最小时间戳可能为负数。

时间戳可能是某些东西过期的时间(比如门票),有些是过去的(也许不是今年之前),有些是未来的。 (也许提前不超过2年。)

时间可以为负数。人类在 1970 年之前登陆月球,因此时间戳将为负数。

public static final String RELEASE_DATE = "2011/06/17"; private static final long MIN_TIMESTAMP; static { try { MIN_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd").parse(RELEASE_DATE).getTime(); } catch (ParseException e) { throw new AssertionError(e); } } // after the software was release and not in the future. public static final boolean validTimestamp(long ts) { return ts >= MIN_TIMESTAMP && ts <= System.currentTimeMillis(); }

打印

String MAN_ON_MOON = "1969/07/21 02:56 GMT"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm Z"); System.out.println(sdf.parse(MAN_ON_MOON).getTime());



0
投票


0
投票

-14159040000

© www.soinside.com 2019 - 2024. All rights reserved.