16

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

4

6 回答 6

29

我结束了使用它,如果他们搜索它,只想将它发布给其他人。

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;
}
于 2012-05-19T01:45:54.580 回答
0

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

于 2012-05-14T07:17:52.463 回答
0

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

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

于 2012-05-14T07:26:57.180 回答
0

只需使用:

Date.compareTo(Date) 

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

于 2012-05-14T07:29:15.060 回答
0

date_part功能可以给您hours或您的兴趣,但是,它是一种,substr因此不能依赖它。您需要将您的timestamp价值转换为unix_timestampextract 经过的小时数、分钟数或自您的timestamptill以来的任何相关内容current_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 年以来经过的总秒数后,可以计算小时或其他的正确值。这可以使用epochwithextract函数来实现。
epoch返回自 以来的总秒数1970-01-01 00:00:00-00。而且,你知道,我们可以通过将它除以 3600 将其转换为小时。

epoch与 一起使用extract

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                |  
----------------+--------------+-----------------------  

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

于 2012-05-14T13:57:20.237 回答
0

使用这个,非常准确:

    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);
于 2020-03-30T09:19:12.170 回答