【问题标题】:find hour or minute difference between 2 java.sql.Timestamps?找到 2 个 java.sql.Timestamps 之间的小时或分钟差异?
【发布时间】:2012-05-14 06:54:43
【问题描述】:

我将java.sql.Timestamp 存储在 postgresql 数据库中作为 Timestamp 数据类型,我想找出存储在数据库中的以分钟或小时为单位的差异到当前的时间戳。这样做的最佳方法是什么?是否有内置方法或者我必须将其转换为 long 或其他什么?

【问题讨论】:

标签: java


【解决方案1】:

我已经用完了,只是想发布给其他人,如果他们搜索它。

public static long compareTwoTimeStamps(java.sql.Timestamp currentTime, java.sql.Timestamp oldTime)
{
    long milliseconds1 = oldTime.getTime();
  long milliseconds2 = currentTime.getTime();

  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  long diffDays = diff / (24 * 60 * 60 * 1000);

    return diffMinutes;
}

【讨论】:

  • 你是最棒的!
  • 很好的解决方案艾莉亚!
【解决方案2】:

使用 UNIX_TIMESTAMP 函数将 DATETIME 转换为小时、分钟和秒的值。
如果您不确定哪个值更大,请使用 ABS 函数。

【讨论】:

    【解决方案3】:

    对于日期添加/删除,这是我的通用功能:

    public static Date addRemoveAmountsFromDate(Date date, int field, int amount) {
        Calendar tempCalendar = Calendar.getInstance();
    
        tempCalendar.setTime(date);
        tempCalendar.add(field, amount);
    
        return tempCalendar.getTime();
    }
    

    “字段”示例:Calendar.HOUR_OF_DAY、Calendar.MINUTE

    【讨论】:

      【解决方案4】:

      只需使用:

      Date.compareTo(Date) 
      

      您必须先将 java.sql.Timestamps 转换为 Date。

      【讨论】:

      • OP 想要以小时和分钟的形式查找到现在为止的时间,但如果日期相等,之前或之后,则不是。
      • 这个答案与问题无关
      【解决方案5】:

      date_part 函数可以给你hours 或者你可能感兴趣,但是它是substr 的一种,因此不能依赖它。您需要将您的timestamp 值转换为unix_timestampextract total 从您的timestampcurrent_timestamp 所经过的小时数、分钟数或任何相关的时间。

      示例

      select age( now(), timestamp '2010-11-12 13:14:15' );  
      //results the interval to be "*1 year 6 mons 2 days 04:39:36.093*"  
      
      select date_part( 'hours', age( now(), timestamp '2010-03-10 02:03:04' ) );  
      // results *4* which is not correct.  
      

      在找到自 1970 年以来经过的总秒数后,可以计算小时或其他的正确值。这可以使用 epochextract 函数来实现。
      epoch 返回自 @987654333 以来的总秒数@。而且,你知道,我们可以通过将它除以 3600 将其转换为小时。

      epochextract 一起使用

      select
        EXTRACT( EPOCH FROM current_timestamp - timestamp '2010-11-12 13:14:15' ) as total_seconds,
        EXTRACT( EPOCH FROM current_timestamp - timestamp '2010-11-12 13:14:15' ) / 3600 as total_hours;  
        ROUND(  
               EXTRACT( EPOCH FROM current_timestamp - timestamp '2010-11-12 13:14:15' ) / 3600  
             ) as total_hours_rounded;  
      

      //结果:

      ----------------+--------------+-----------------------  
      | total_seconds | total_hours  | total_hours_rounded  |  
      | --------------+--------------+----------------------|  
      | 47452282.218  | 13181.189505 | 13181                |  
      ----------------+--------------+-----------------------  
      

      同样,我们可以提取其他需要的值,按需使用。

      【讨论】:

        【解决方案6】:

        用这个,非常准确:

            long diff = end - start;
        
            long diffDays = diff / (24 * 60 * 60 * 1000);
            long remain = diff % (24 * 60 * 60 * 1000);
        
            long diffHours = remain / (60 * 60 * 1000);
            remain = remain % (60 * 60 * 1000);
        
            long diffMinutes = remain / (60 * 1000);
            remain = remain % (60 * 1000);
        
            long diffSeconds = remain / (1000);
        
            System.out.println("days : " + diffDays);
            System.out.println("hours : " + diffHours);
            System.out.println("minutes : " + diffMinutes);
            System.out.println("secs: " + diffSeconds2);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-15
          • 1970-01-01
          • 2010-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多