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

发布于 2024-11-24 06:18:53 字数 253 浏览 1 评论 0原文

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

例如,如果给定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-12-01 06:18:53

我们无法告诉您什么对您的应用来说是合理的。如果有一个限制对于每种情况都是正确的,那么它将被内置。可能只有开发应用程序之后的时间戳才是明智的,而不是未来的时间戳。

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();
}

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

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


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

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

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.

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();
}

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.

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());

prints

-14159040000
凉城凉梦凉人心 2024-12-01 06:18:53

为什么不直接从给定的时间中减去纪元时间呢?如果结果为负,则无效。

Why not just subtract the epoch time from the time you're given. If the result is negative, then it's not valid.

兲鉂ぱ嘚淚 2024-12-01 06:18:53

很老的问题,但我是这样做的:

   boolean isNumberValue(String value) {
      //TODO - check if is number
      String regex = "^[0-9]+$";
      Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(value);
      boolean matchFound = matcher.find();

      if(matchFound) {
           //System.out.println("Match found");
           return true;
      } else {
          //System.out.println("Match not found");
          return false;
      }
   }

   boolean isTimestamp(String timestamp) {      
        try {
            long time = Long.valueOf(timestamp);
            Date date = new Date((long)time);
            long dateOld = date.getTime();
            //if no exception was raised, then its unix and only digits
            if(!isNumberValue(String.valueOf(dateOld))) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
      
   }

   if (isTimestamp(1679308838)){
       System.out.println("Is unix timestamp");
   }else{
       System.out.println("NOT unix timestamp");
   }

Very old question but here is how I did it:

   boolean isNumberValue(String value) {
      //TODO - check if is number
      String regex = "^[0-9]+
quot;;
      Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(value);
      boolean matchFound = matcher.find();

      if(matchFound) {
           //System.out.println("Match found");
           return true;
      } else {
          //System.out.println("Match not found");
          return false;
      }
   }

   boolean isTimestamp(String timestamp) {      
        try {
            long time = Long.valueOf(timestamp);
            Date date = new Date((long)time);
            long dateOld = date.getTime();
            //if no exception was raised, then its unix and only digits
            if(!isNumberValue(String.valueOf(dateOld))) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
      
   }

   if (isTimestamp(1679308838)){
       System.out.println("Is unix timestamp");
   }else{
       System.out.println("NOT unix timestamp");
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文