Date and Time in JPQL and Criteria Queries
Date and time expressions may appear in JPQL queries:
- asdate and time literals - e.g.
{d '2011-12-31'},{t '23:59:59'}. - asparameters- when date and time values are assigned as arguments.
- as path expressions - in navigation to persistent date and time fields.
- as results of predefined JPQL current date and time functions.
This page covers the following topics:
Current Date and TimeExtracting Date PartsDate and Time in Criteria QueriesCurrent Date and Time
JPA defines special JPQL expressions that are evaluated to the date and time on the database server when the query is executed:
CURRENT_DATE- is evaluated to the current date (ajava.sql.Dateinstance).CURRENT_TIME- is evaluated to the current time (ajava.sql.Timeinstance).CURRENT_TIMESTAMP- is evaluated to the current timestamp, i.e. date and time
(a java.sql.Timestampinstance).
Extracting Date Parts
JPA doesn't define standard methods for extracting date and time parts but some JPA implementations, as well as ObjectDB, support such functions as an extension. ObjectDB supports 6 functions for extracting theYEAR,MONTH,DAY,HOUR,MINUTE andSECOND.
For example:
YEAR({d '2011-12-31'})is evaluated to2011.MONTH({d '2011-12-31'})is evaluated to12.DAY({d '2011-12-31'})is evaluated to31.HOUR({t '23:59:00'})is evaluated to23.MINUTE({t '23:59:00'})is evaluated to59.SECOND({t '23:59:00'})is evaluated to0.
Date and Time in Criteria Queries
TheCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings. interface provides three factory methods for building date and time expressions that represent the current date and/or time:
// Create current date expression:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<javax.sql.Date> date= cb.currentDateCriteriaBuilder.currentDate()-JPAMethodCreate expressiontoreturn current date.();// date only// Create current time expression:Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<javax.sql.Time> time= cb.currentTimeCriteriaBuilder.currentTime()-JPAMethodCreate expressiontoreturn current time.();// time only// Create current date & time expression:Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<javax.sql.Timestamp> ts= cb.currentTimestampCriteriaBuilder.currentTimestamp()-JPAMethodCreate expressiontoreturn current timestamp.();// both
Unlike constant date literals which are built once on the client side, the current date and time expressions are re-evaluated on the server on every query execution to reflect the date and time when the query is run.
Functions for extracting date and time parts are also available in criteria queries by using the genericCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings.'sfunctionCriteriaBuilder.function(name,type,args) - JPA Method Create an expression for the execution of a database function. method, as follow:
// Create expressions that extract date parts:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> year= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("year",Integer.class, date);Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> month= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("month",Integer.class, date);Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> day= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("day",Integer.class, ts);// Create expressions that extract time parts:Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> hour= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("hour",Integer.class, time);Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> minute= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("minute",Integer.class, time);Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> second= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("second",Integer.class, ts);