【问题标题】:Compare System.currentTimeMillis()比较 System.currentTimeMillis()
【发布时间】:2016-08-07 10:51:30
【问题描述】:

我想使用System.currentTimeMillis()比较两个不同的时间戳。

基本上我想检查时间戳是否在特定日期的 3 小时范围内。

如何做到这一点?

【问题讨论】:

  • 你能在某一天展开吗?时间戳没有“天”的概念,您还需要一个时区。如果在给定的时区,8 月 7 日凌晨 1 点和 8 月 6 日下午 23 点会发生什么?
  • 啊,上面的评论当然应该是和8月6日晚上11点...
  • 长时间1,时间2。 ( (time1 - time2) / (60 * 60) ) // 秒 * 分钟。应该让你知道他们之间的时间间隔

标签: java date time compare


【解决方案1】:

考虑到您有time1time2,其中time1 <= time2 您可以这样做:

if (time1 >= time2-(60*60*3*1000)) {
   // interval is up to 3 hours
} else {
   // interval is more than 3 hours
}

【讨论】:

    【解决方案2】:

    使用Instant获取epoch中的时间,转换为LocalDateTime获取当天信息,查看第一次加3小时是否小于第二次:

    long millis1 = System.currentTimeMillis();
    ...
    long millis2 = System.currentTimeMillis();
    
    Instant instant1 = Instant.EPOCH.plusMillis(millis1);
    Instant instant2 = Instant.EPOCH.plusMillis(millis2);
    
    LocalDateTime t1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
    LocalDateTime t2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());
    
    System.out.println("same day: " + (t1.getDayOfYear() == t2.getDayOfYear()));
    System.out.println("t1+3h >= t2: " + (t1.plusHours(3).compareTo(t2) >= 0));
    

    【讨论】:

    • 如果我想从数据库表中选择行,其中有一列存储了 currentTimeMIllis() 值。如何使用 sql 查询执行上述操作?
    • 您只需从数据库中获取long 值,而不是System.currentTimeMillis。此外,您可能需要调整 ZoneId.systemDefault() 以使用不同的时区,例如如果您指的是EDT(UTC-4:00),但在欧洲执行代码...
    • 使用Instant.ofEpochMilli 更简单。在分配时区时使用ZonedDateTimeLocalDateTime 的目的是丢失时区信息。
    【解决方案3】:

    这里有一个示例程序,可能会对您有所帮助。这里的关键是“findIfDatesWithinThreeHours”方法,它有助于确定两个实例是否相隔三个小时。

    package com.inrvu.adapter;
    
    import java.util.Calendar;
    import java.util.Date;
    
    
    public class Tester {
    
    public static void main(String[] args) {
        Date[] dates = getTwoDatesThreeHoursApart();
        System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));
    
        dates = getTwoDatesWithinThreeHours();
        System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));
    }
    
    public static Date[] getTwoDatesThreeHoursApart()
    {
        Calendar calendar = Calendar.getInstance();
        Date date1 = calendar.getTime();
        calendar.add(Calendar.HOUR,4);
        Date date2 = calendar.getTime();
        return new Date[]{date1,date2};
    }
    
    public static Date[] getTwoDatesWithinThreeHours()
    {
        Calendar calendar = Calendar.getInstance();
        Date date1 = calendar.getTime();
        calendar.add(Calendar.HOUR,3);
        Date date2 = calendar.getTime();
        return new Date[]{date1,date2};
    }
    
    public static boolean findIfDatesWithinThreeHours(Date date1, Date date2) {
        // here the time of a single hour is defined as 60*60*1000
        System.out.println(date1);
        System.out.println(date2);
        return Math.abs(date1.getTime()-date2.getTime()) <= 3*60*60*1000;
    
    }
    }
    

    【讨论】:

      【解决方案4】:

      您的问题含糊不清。这可能会为您指明正确的方向。

      如果“时间戳”是指自 1970 UTC 纪元以来的毫秒数,construct an Instant。此类表示 UTC 时间轴上的时刻,分辨率为纳秒(小于毫秒)。

      Instant instant = Instant.ofEpochMilli( millis );
      

      获取当前时刻。

      Instant now = Instant.now();
      

      计算三个小时。

      Instant in3Hours = now.plus( 3 , ChronoUnit.HOURS );
      

      看看你的时刻是否在现在和三个小时后。

      Boolean contained = ( ( ! instant.isBefore( now ) ) && instant.isBefore( in3Hours ) );
      

      如果您想比较一对时刻以查看经过的时间是否少于 3 小时,请使用 Duration。此类表示以秒和纳秒为单位的时间跨度。

      Instant earlierInstant = … ;
      Instant laterInstant = … ;
      Duration duration = Duration.between( earlierInstant , laterInstant );
      if ( duration.isNegative() ) { 
          … handle error of unexpected data inputs where the second instant is *before* the first instant.
      }
      … else …
      Boolean elapsedUnderThreeHours = ( duration.compareTo( Duration.ofHours( 3 ) ) == -1 );
      

      【讨论】:

        猜你喜欢
        • 2017-06-25
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多