Calculation of the date difference of hql in Hibernate, the number of seconds to calculate the difference

Source: Internet
Author: User

A recent business need is to calculate the order creation time is now more than 4 hours of orders are found out! Then we need to use the DATE function. I got a look on the net. The total date function has a few:

Current_date () Returns the current date of the database Time function JPAQL HQL Current_date () Returns the current date of the database
Current_time () Time Time function JPAQL HQL Current_time () Returns the current time of the database
SECOND (d) Extract a specific second from a date Time function HQL SECOND (Time field) Empty when NULL is returned
MINUTE (d) Extract specific points from the date Time function HQL MINUTE (Time field) Empty when NULL is returned
HOUR (d) Extracting specific hours from a date Time function HQL HOUR (Time field Empty when NULL is returned
Day (d) Extract a specific day from a date Time function HQL Day (Time field) Empty when NULL is returned
MONTH (d) Extract specific months from a date Time function HQL MONTH (Time field) Empty when NULL is returned
Year (d) Extracting a specific year from a date Time function HQL Year (Time field) Empty when NULL is returned

Obviously this can not meet the demand, Current_date finally came to the result is: 2015-07-18 and current_time () The result is: 14:40:58, not a date type, and then I Baidu a bit found this function current_ TIMESTAMP (), make sure it is available. The return is: 2015-07-18 14:40:58, OK. Meet the needs of the present. The next step is to calculate the difference between the two date types:

The question came: Current_timestamp ()-Createtime, when Current_timestamp () was 2015-07-18 14:42:55,createtime for: 2015-07-15 10:30:21, returned the result is actually: 3041234, cannot believe own eyes. In fact, this value is also regular to follow, every 2 digits of a month and a day and seconds.

So, 34: The difference is 34 seconds.

12:12 points difference

04:4 Hours Difference

And so on

Of course there are the following values: 2961512. How does that make sense? First, 12 seconds, 15 points well understood, ran out a 96?   Is it 96 hours? Of course the answer is wrong, because when the settlement of small time difference, the value of the deduction is less than the meiosis, then it must be borrow, that is, the algorithm is: 24-(100-96) = 20, 20 hours, the same time if the minute is 81, then the difference minutes are: 60-(100-81) =41 points, and so on.

Basic algorithm understand, then the problem is good to solve, the difference of 4 hours, then only need to meet (Current_timestamp ()-Createtime) >40000 can, 4 minutes only need to meet (Current_timestamp ()- Createtime) >400, others, etc.!

I can not find a better way to calculate the difference of two date types, only with this stupid method, do not know that users can have other good solutions for reference Reference! I believe the demand is still very large. The reason for this problem is also because the project cannot be caused by native SQL.

Calculation of the date difference of hql in Hibernate, the number of seconds to calculate the difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.