Jasper Reports - Add one day to a Date Parameter - jasper-reports

I'm creating a Jasper report that includes the following parameters:
DATESTART (Date)
DATEEND (Date)
These parameters indicate a date range for a field called DATECREATED (Timestamp) which includes times. I would like the date range to be INCLUSIVE, that is, if I filter for "Jan 1, 2009" to "Jan 31, 2009", any DATECREATED value on Jan 31, 2009 (such as "Jan 31, 2009 15:00") will be included in the report.
When I used Crystal Reports in the past, I used the DATEADD function to create a filter expression like the following:
{DATECREATED} >= {DATESTART} and {DATECREATED} < DATEADD("d", 1, {DATEEND})
(I realize that this isn't syntactically correct, but you get the idea.)
Is there any way to do something similar in Jasper Reports?

If you understand French, there the same question is asked in this thread
(the only difference is that it is about adding a month)
The proposed solutions are following:
SQL
Do it with SQL statement directly in the query (if your data source is a SQL datasource of course).
With MySQL you can do something like
DATE_ADD($P{DATEEND},INTERVAL 1 DAY);
more information: Date and Time Functions (MySQL doc)
JAVA
The other solution is to use the Java possibly of the Date object:
I proposed something like :
$P{DATEEND}.setDay($P{DATEEND}.getDay()+1)
But I did not try it (and it is probably wrong).
Maybe you need to defined a new Date Variable DATEEND_1
with a value expression like :
new Date($P{DATEEND}.getTime() + 24*60*60*1000)
or
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)
And use this new variable in your query V{DATEEND_1}.
(again I am not sure of it)

Try this:
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)

Another option is to use the Groovy SDK that comes bundled with the latest versions.
new Date().plus(1) //Today plus one day = tomorrow.
Or to be more complete - add your two parameters DATESTART and DATEEND and set the default expression to be the above code. Then in your query add the following to the where clause:
DATECREATED >= $P{DATESTART} and DATECREATED < $P{DATEEND}
or depending on your SQL variant.
DATECREATED BETWEEN $P{DATESTART} AND $P{DATEEND}

Suppose you have a Parameter PARAM1 and you want to add 366 days to param1 then you can do it by following way
1) Declare another parameter say $P{finalDate}
2) Code like below for $P{finalDate} as the Default Value Expression
new Date($F{PARAM1}.getTime() + 366L*24*60*60*1000)
Dont forget to put L after 366 . Without putting L , it may not work properly and may not give accurate date.
It will add 366 days to PARAM1

We have built a library of static date-related functions, which can be used like this:
DateUtil.add(NOW(), 0, 0, 1)
The above would get you a date one day into the future (the arguments are year, month, day). It would be nice to have a DATE_ADD function supported directly by JasperReports. Sounds like a patch waiting to be submitted.

DAYSINMONTH($P{Date}) >= (DAY($P{Date})+ 1)
?
DATE(YEAR($P{Date}),MONTH($P{Date}),DAY($P{Date})+1)
:
((MONTH($P{Date}) + 1) > 12) ? DATE(YEAR($P{Date}) + 1,1,1) : DATE(YEAR($P{Date}),MONTH($P{Date}) +1 ,1)
I am sorry for necro'ing this post but I thought that I should share this as another alternative to the options posted above. You just need to change all the $P{Date} to be your parameter!

You can create a class to help you, which has a static method which processes your parameters (or modify class JasperUtils by adding a static method). This way you can have amethod returning a boolean which will act exactly as the filter you need.
package com.package_name.utils;
public class JasperUtils2 {
public static Boolean filterDate(Date dateStart, Date dateEnd, Date dateCreated) {
if (dateCreated.compareTo(dateStart) < 0) return false; // dateCreated is greater or
// equal to dateStart
if (dateCreated.compareTo(dateEnd) > 0) return false; // dateCreated is smaller or
// equal to dateEnd
// you can combine the two conditions into one. I wrote it like this to be more obvious
}
}
To use this method, you need to import the class created (Edit -> Report import directives -> new import ---- this may differ for different versions of iReport). Then, you can use the method in static way:
JasperUtils2.filterDate(..., ..., ...)

Related

SSRS exact month difference

This may have been asked before but I've not been able to find it having searched! In Oracle SQL there's a function called MONTHS_BETWEEN which returns a fractional value if the two dates you are comparing are not both the first day of the month for example. I need to do something similar in SSRS report builder, I've tried using DateDiff("m",Date1,Date2) however this always returns an integer and I think from what I can tell it just compares the two months from the dates, so when I compare 30/09/20 and 01/04/21 I get 7 months when actually it is much closer to 6.
Is there a function or a fix that can be used in SSRS to get that more accurate value?
Thank you!
For example I would like to get the following result:
Difference between 30/09/20 and 01/04/21 = 6.1
Difference between 01/08/20 and 30/09/20 = 1.9
It doesn't have to super accurate as I will be rounding to the nearest integer but I'm looking for something that will recognise that in the second example nearly 2 months have been covered and in the first example it's only just over 6 months.
If you only need an approximation then you could just calculate the number of days difference and divide by 30.
using the following expression...
=DATEDIFF("d", Fields!startDate.Value, Fields!endDate.Value)/30
I put a few more examples into a table and got the following results.
The following code mimics oracle definition of months_between
Public Function MonthsBetween( d1 As Date, d2 As Date) As Decimal
Dim df As Decimal
df = DateDiff("m", d1, d2)
If Day(d1) <> Date.DaysInMonth(Year(d1), Month(d1)) Or Day(d2) <> Date.DaysInMonth(Year(d2), Month(d2)) Then
df = df + Cdec((Day(d2)-Day(d1))/31)
End If
Return df
End Function
Integer result when both dates are last day of month
Negative result when date1 > date2
Decimal part based on 31 days month
For your expression use something like
=Code.MonthsBetween(Fields!date1.Value , Fields!date2.Value)
UPDATE
The following expression works in the same manner
= Cdec(
DateDiff("m", Fields!date1.Value, Fields!date2.Value)
+
Iif (
Day(Fields!date1.Value) <> Date.DaysInMonth(Year(Fields!date1.Value), Month(Fields!date1.Value)) Or
Day(Fields!date2.Value) <> Date.DaysInMonth(Year(Fields!date2.Value), Month(Fields!date2.Value)) ,
Cdec( (Day(Fields!date2.Value) - Day(Fields!date1.Value))/31),
0.0
)
)

Calculate the days between two fields in sugarCRM

How to calculate number of days between two date fields of same module without counting the weekends (Saturday and Sunday) using the "Formula Builder" in sugarCRM studio.
Preliminary remarks
As far as I'm aware Sugar Logic doesn't have a function to count the days of a date span.
However we can calculate it using existing Sugar functions like this:
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
This will return the span from $date_start and $date_end, counting all days, including both start and end date,
so the span 2017-01-01 to 2017-01-01 will return 1 rather than 0.
Notes:
This does not handle the special cases of any of those fields being empty.
If you want to display the result of this formula directly, wrap it in floor() to display as an integer without .000000
The solution
Since Sugar Logic also does not seem to provide any modulo function and formula scoped variables either,
the resulting formula for what you want (count only Mo-Fr) is as "compact" as:
floor(add(0.5,
add(
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),5)
,
add(
min(
max(0,subtract(6,ifElse(equal(dayofweek($date_start),0),7,dayofweek($date_start))))
,
subtract(
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
,
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),7)
)
)
,
max(
0,
subtract(
subtract(
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
,
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),7)
)
,
subtract(8,ifElse(equal(dayofweek($date_start),0),7,dayofweek($date_start)))
)
)
)
)
))
Notes:
The formulas above do not handle start dates that are after the end date, you could catch such cases using if() and isAfter()
Holidays and such are not considered at all, so this will not only count actual work days

Sum a value in SSRS Reports based on date range

I'm trying to sum a field value in an SSRS report based on a date range field in the same dataset, but have had no luck. So far this is what I've come up with. I've scoured the forums with no luck. Any help would be greatly appreciated.
= sum( iif(
datediff("d",Fields!Calculated_DueDate.Value.value,today())>=31
AND
(datediff("d",Fields!Calculated_DueDate.Value,today())<62
AND (Fields!Calculated_InvoiceBal.Value.value>0 ),
Fields!Calculated_InvoiceBal.value,0)))
Calculated_DueDate is my date field and
Calculated_InvoiceBal is the field I want to sum
The first: you shouldn't use expression like Fields!Calculated_DueDate.Value.value - it is error.
The second: There are error in arrangement of brackets.
The third: In SSRS 2008R2 call of function today() marks as error. You can use expression DateTime.Today to get current date without errors markup.
So, your expression should look like this:
=sum(
iif(datediff("d",Fields!Calculated_DueDate.Value, DateTime.Today) >= 31
AND datediff("d",Fields!Calculated_DueDate.Value, DateTime.Today) < 62
AND (Fields!Calculated_InvoiceBal.Value > 0),
Fields!Calculated_InvoiceBal.value,
0
)
)
Try this:
=SUM(IIf(datediff("d",Fields!Calculated_DueDate.Value.value,today())>=31 AND datediff("d",Fields!Calculated_DueDate.Value,today())<62 AND Fields!Calculated_InvoiceBal.Value.value>0),
Fields!Calculated_InvoiceBal.value, 0)

How to make date comparison in yii framework

I am creating project in yii. I am having Poll table with the fields as-
-pollId
-pollQustion
-isActive
-publishDate
I want to check weather publishdate of poll is not greater than current date.
I am implementing as-
$CurrrentDate=new CDbExpression('NOW()');
if($record->publishDate < $CurrrentDate))
{
some code......
}
But its not executing correctly. Code is executing even if publish date is greater than current date. So how to make this comparison in yii framework. please help me
PHP do not have a date format. When you compare $CurrentDate and $record->publishDate you compare strings. Well, for right compare you need to convert it to Unix's timestamp
// here you set right date/time pattern
// You need to explain CDateTimeParser for your pattern
// just easy use `strtotime` if you have 2012-12-12 12:12:12
$publishTimestamp = strtotime($record->publishDate);
// $publishTimestamp = CDateTimeParser::parse($record->publishDate, 'yyyy-MM-dd hh:mm:ss');
// if you need to use Hours and minutes just easy use `time()`
$currentDateTimestamp = time();
// $currentDateTimestamp = strtotime(date('Y-m-d H:i:s'));
// now we can compare
if ($publishTimestamp < $currentDateTimestamp) {
// do some
}

What type should you store the date in a database?

Currently I'm storing it as a String, but got problems using it when it comes to querying by date with GQL.
Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss, z");
String todayDate = formatter.format(date);
The query:
"SELECT FROM SomeTable p WHERE date = 01/01/2011"
Error:
Exception:
org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreFeatureException:
Problem with query : Right side of expression is
composed of unsupported components.
Left:
org.datanucleus.query.expression.Literal,
Op: / , Right:
DyadicExpression{Literal{5} /
Literal{11}}
How can I search by date?
Always as a date - the database should check on the types of data that it stores thus making sure the data is what it says. the information about what type is known on the insert so check it then rather than later when none has any idea of what the correct value should have been.
the error you are getting is probably not due to this but because the date in the query needs to be made a date if types are correct or if a string needs to be as a single-quoted string see GQL Reference
e.g.
"SELECT FROM SomeTable p WHERE date = '01/01/2011'"
but what date is 06/07/2011 ? I think it is today 6th July, others 7th June. So even if you use strings use the ISO format 2011-07-06 A date type will hide this detail making it easier. Note that GQL only uses the ISO form so even if you got the command to have the date in a string it would fail.
but better as
SELECT FROM SomeTable p WHERE date = DATE( 2011, 1, 1)