我在的PostgreSQL数据库存储java.sql.Timestamp
为时间戳数据类型,我想找出从存储在数据库中,以当前时间戳了一个在几分钟或几小时的差异。这样做的最好方法是什么?有没有建立在它的方法或我必须将其转换为长或什么?查找2个java.sql.Timestamps之间的小时或分钟差异?
回答
我结束使用这个,只是想发布给他人,如果他们搜索它。
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;
}
你是最棒的! – dbrad
使用UNIX_TIMESTAMP
函数将DATETIME转换为以小时,分钟和秒为单位的值。
如果您不确定哪个值更大,那么使用ABS功能。
对于日期添加/删除这是我的泛型函数:
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
只需使用:
Date.compareTo(Date)
你必须先将java.sql.Timestamps转换为Date。
OP希望以小时和分钟的形式查找到目前为止的时间,但如果日期相等,则前或后都不会。 –
这个答案与问题无关 –
date_part
功能可以给你hours
或作为利益可能是,但它是一种substr
,因此不能依靠它。您需要将您的timestamp
值转换为unix_timestamp
和extract
总计经过的小时数,分钟数或自timestamp
至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年以来经过的秒数这可以通过使用与epoch
功能extract
来实现总数后进行计算。
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 |
----------------+--------------+-----------------------
同样,我们可以提取其他所需的值并根据需要使用。
- 1. 以小时和分钟查找两个日期之间的差异
- 2. 查找小时之间的差异
- 3. 2个大查询时间戳字段之间的分钟差异
- 4. C++ chrono秒钟内2个时钟之间的差异
- 5. 使用php或mysql查找时间差异在几分钟内
- 6. 2小时之间的差异?
- 7. 如何计算两个Java java.sql.Timestamps之间的差异?
- 8. 2个时间戳之间的差异
- 9. 两分钟之间的差异
- 10. 小数格式的2个日期时间之间的差异
- 11. 2个定时器之间的差异
- 12. 在分钟查找时间差异powershell脚本
- 13. 2天之间的差异在几分钟内
- 14. 找到两个日期时间之间的微小差异
- 15. 时间戳在分钟之间的差异
- 16. 2角之间的最小差异
- 17. 添加或找到两个时间戳之间的差异
- 18. MySQL日期时间在天,小时和分钟上的差异
- 19. 2时间戳之间的差异 - PHP
- 20. 查找2个类别之间的差异
- 21. 查找2个词典之间的差异
- 22. Python - 查找2个数字之间的差异
- 23. 计算2日期和时间之间的小时和分钟
- 24. 查找两个数组之间的最小差异
- 25. 查询找到两个时间数据之间的差异
- 26. 查找两个时间戳之间的差异
- 27. 找到时间之间的差异c
- 28. 的UIDatePicker - 以小时为单位2个日期之间的差异:分
- 29. 查找时间差异
- 30. 2个时间戳之间的日期和时间差异?
的可能重复[如何计算两个Java java.sql.Timestamps之间的区别?(http://stackoverflow.com/questions/582278/how-to-calculate-the-difference-between- two-java-java-sql-timestamps) –