如何在java中验证unix时间戳?
我需要验证给定的输入字符串是否为有效的时间戳(以毫秒为单位)。
例如,如果给定 Timestamp
String time ="1310966356458";
那么它应该返回 true。
如果
String time ="1000";
那么它应该返回 false;
请帮忙。提前致谢
I need validate the given input String is a valid Timestamp
in milliseconds.
For example if the given Timestamp
String time ="1310966356458";
Then it should return true.
if
String time ="1000";
then it should return false;
Please help. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们无法告诉您什么对您的应用来说是合理的。如果有一个限制对于每种情况都是正确的,那么它将被内置。可能只有开发应用程序之后的时间戳才是明智的,而不是未来的时间戳。
然而,时间戳可能代表某人的出生时间。在这种情况下,最小时间戳可能为负数。
时间戳可能是某些东西过期的时间(例如门票),有些是过去的(也许不是今年之前),有些是未来的。 (也许提前不超过 2 年。)
时间可以为负数。人类在 1970 年之前登陆月球,因此时间戳将为负数。
印刷
We cannot tell you what is sensible for your application. If there was a limit which was correct for every situation it would be built in. It could be that only timestamps after you developed your application and not in the future are sensible.
However, it could be that the timestamp represents when someone was born. In which case the minimum timestamp could be negative.
It could be that the timestamp is the time when something expires (like tickets) Some will be in the past (perhaps not before this year) and some will be in the future. (perhaps not more than 2 years in advance.)
Times can be negative. Man landed on the moon before 1970 so the timestamp would be negative.
prints
为什么不直接从给定的时间中减去纪元时间呢?如果结果为负,则无效。
Why not just subtract the epoch time from the time you're given. If the result is negative, then it's not valid.
很老的问题,但我是这样做的:
Very old question but here is how I did it: