Jump to content

number of days between two dates


donbowden

Recommended Posts

Hi everyone!

I need to have a conditional statement that finds the number of days between two dates.  If the second date is null, then  it should calculate the the number of days between the first date and the current date, but if the second date is not null then return the actual number of days between the two dates.  From what I can determine, I know I need to manipulate the data types but I have not been able to find the right syntax to make it work. 

($F(DateType2) != null) ? New Date() - $F(DateType1)  : $F(DateType2) - $F(DateType1)

Much thanks for any help in resolving this issue.

Don

 

Code:
($F(DateType2) != null) ? New Date() - $F(DateType1)  : $F(DateType2) - $F(DateType1)
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Possible solution:

 

  new Integer( (int) ( ($F{DateType2}.getTime() - $F{DateType1}.getTime()) / (1000*60*60*24) )

This calculate the date difference in millisecs and return the integer result.
It does not consider partial days and so on.

Another solution is to write a custom helper class to perform the calculation with more accuracy.

Giulio

 

Link to comment
Share on other sites

Hi Giulio,

Thank you for your response.  I attempted your line of code and received the following error.  First let's review the actual code used with the actual field names:

new Integer( (int) ( ($F{MORTAPPLYDATE}.getTime() - $F{CONTRACTDATE}.getTime()) / (1000*60*60*24) )

Next is the following error as reported in the "Report Problems Window":

Errors compiling N:JasperSoft iReportReportsProspectMasterList9.jasper!
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector@830ed5  net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_ProspectMasterList_1239763427882_470676: 284: unexpected token: } @ line 284, column 9. 1 error      at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:92)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:192)     at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:219)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:135)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:538)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:561)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:986)
Compilation running time: 391!
 

Thank you again for your asistance.  BTW, I have purchased and read your book and it provided much assistance.

Regards,

Don

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...