How to add minutes to a Date with JPA2

#1

Hi, 

I have to sum to a date of minutes, and check the date obtained is greater than the current date.

My idea is as follows:

Calendar currentTime = Calendar.getInstance();
Date currenteDate = currentTime.getTime();
Expression<Date> dateCapture = root.get(Counter_.dateCapture);

Expression<Integer> samplingPeriod = root.get(Counter_.samplingPeriod);
Expression<Integer> delayMinutes  = cb.prod(samplingPeriod,3); //minutes to be added

I must now sum to the dateCapture the delayMinute

and verify that the current date is less than dateCapure+delayMinutes,

cb.greaterThan(dateCapture+delayminutes, currenteDate);

How to do!!

#2

Wouldn't it be more simple to subtract the delay from the current date and send the result as an argument for direct comparison with the date field?

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

 

Yes, good idea but my problem is to use the greater than subjects with Integer and Date 

Calendar currentTime = Calendar.getInstance();
long currentDate = currentTime.getTime().getTime();
Expression<Date> dateCapture = counterJoinValue.get(Value_.dateCapture);
Expression<Integer> samplingPeriod = root.get(Counter_.samplingPeriod);
Expression<Integer> delayMinute  = cb.prod(samplingPeriod,3);


Expression<? extends Number> dateDelay = cb.diff(currentDate, delayMinute);
Predicate predicateCounterNotUpdate = cb.greaterThan(dateCapture, dateDelay); //is not applicable for the arguments Expression<Date>, Expression<capture#20-of ? extends Number>)
  predicate = cb.and(predicate, predicateCounterNotUpdate);

 

 

 

#4

This is an interesting question. It is unclear if this is supported in standard JPA.

It is possible to add to ObjectDB as an extension another date function that will return time in milliseconds representation, and you will be able to work with numbers (milliseconds) rather than with dates.

But this will not be a portable solution. 

 

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

Ok, thanks. I also after several failed attempts, I decided to write a function on the database and retrieve it with the data function. This will not be a portable solution, but for now it should work.

I'll let you know.

#6

Sorry for the delayed response but I solved this way, a function that does all the database. 

I write the solution could help someone 

Calendar currentTime = Calendar.getInstance();
  Date currentDate = currentTime.getTime();
    
  Expression<Date> date = counterJoinValue.get(Value_.date);
  Expression<Integer> period = root.get(Counter_.Period);
  Expression<Integer> delayPeriod = cb.literal(IConstants.DELAY_PERIOD);
 Expression<Date> dateDelay = cb.function("DATE_DELAY", Date.class,date,period,delayPeriod);


Predicate predicate = cb.lessThan(dateDelay,currentDate);

This is the function DB

create or replace
FUNCTION DATE_DELAY
(
  DATE IN DATE 
, PERIOD IN number
, DELAY_PERIOD IN number
) RETURN DATE
IS data_delay date:=null;
BEGIN
select DATE+((PERIOD*DELAY_PERIOD)/1440) into data_delay
from dual;
  RETURN data_delay;
END DATE_DELAY;

And bye bye ;-)

 


Post Reply

To post a reply and/or subscribe to update notifications - please login