Is there any way to remove time to compare dates only in jasper? - jasper-reports

I have 2 dates.
1st is
<field name="duedate" class="java.util.Date"/>
& second is current date
so I am doing something like this
<![CDATA[ $F{duedate} >= (new java.util.Date()) ? "one" : "two"]]>
which doesn't work if dueDate matches new java.util.Date()
also seems there is some problem with date format. I am using PostgreSQL query to get the date.
Say query is like this
<queryString>select dueDate from table1</queryString>
& then I get that field like this
<field name="duedate" class="java.util.Date"/>
but PostgreSQL dueDate o/p is of type java.sql.date
So field that I declared, will that work as util.date?

java.util.Date has it's own comparison methods ( compareTo(), after(), before() ), and they work in Jasper as well. So you might try something like:
$F{duedate}.compareTo(new java.util.Date() == 0 ? "one" : "two"
Another possible solution i might think of right now is to make the comparison inside the query of your report, like this:
SELECT
duedate as duedate,
CASE WHEN duedate == sysdate THEN "one"
ELSE "two" END as comparison
FROM ....

Related

Create an inserted_at datetime where filter using simple date string

I'm trying to get records inserted after a certain date given to me by the client.
2018-06-06
Here's how I'm writing the query:
{:ok, date} = NaiveDateTime.from_iso8601(date_string)
from(
m in query,
where: m.inserted_at > ^date
)
(MatchError) no match of right hand side value: {:error, :invalid_format}
And when I try to use a simple Date object:
** (Ecto.Query.CastError) lib/messages/search.ex:77: value ~D[2018-06-06] in where cannot be cast to type :naive_datetime in query
How can I find all messages inserted after that dummy string date the client is passing me?
You have an ISO 8601 date there, not a datetime. You can convert it into a NaiveDateTime (with hour, minute, second all set to 0) like this:
iex(1)> date_string = "2018-06-06"
"2018-06-06"
iex(2)> ndt = NaiveDateTime.from_iso8601!(date_string <> " 00:00:00")
~N[2018-06-06 00:00:00]
Now you can use ndt in your query and it will work.

Convert packed DB2 iseries value to YYYY-MM-DD

I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;

Comparing date value with YEAR in Tableau

I've a calculated field in Tableau which has the Years (Date Value) from a date field. When I compare this calculated field with year of another field, I get error.
IF calculated_Field = YEAR(order_date)
...
1) calculated_Field is the one created using Date value of another field.
2) Order_date is a datetime field.
Error I see in the above IF statement says "Cant compare YEAR and INT values".
When I solved that using below statement, it does not work as expected as IF returns FALSE.
IF INT(calculated_Field) = YEAR(order_date)
Ensure the comparisons are both from YEAR()
IF YEAR(another_field) = YEAR(order_date)
calculated_Field is created using Date value of another_field.
Order_date is a datetime field.

Retrieving date from database in jaser

I have date as datetime in mysql. But when I am retriving value in jasper I am getting null pointer.
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : new java.text.SimpleDateFormat("YYYY-MM-DD HH:MI:SS").format($F{birthDate})
<field name="birthDate" class="java.util.Date">
<fieldDescription>
<![CDATA[new java.text.SimpleDateFormat("YYYY-MM-DD HH:MI:SS").format($F{birthDate})]]>
</fieldDescription>
</field>
Please help me how do I get the correct date.
According to documentation there are no MI pattern in «Date and Time Patterns». So, if you want to define the minutes then you must use m or mm patern. Also you must use y patern for year, d or dd patern to define the days in month. And use s or ss for seconds, because S is for milliseconds:
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format($F{birthDate})

Jasper Reports - Add one day to a Date Parameter

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(..., ..., ...)