我需要以毫秒为单位验证给定的输入字符串是否有效。
例如,如果给定
Timestamp
Timestamp
那么它应该返回true。
如果
String time ="1310966356458";
那么它应该返回 false;
请帮忙。预先感谢
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());
-14159040000