How to make Jasper Reports programmatically determine the Name of Columns within the report it self? - jasper-reports

I am generating a report with that will have a 7 columns where the last 6 should have the the last 6 months listed. So as of the time of this writing it should be:
NAME -> September -> August -> July -> June -> May -> April
ss the column headers. I am trying to avoid having to pass them in as parameters, and am trying to get Jasper Reports to figure it out at runtime. I can get the first month pretty easily using a Text Field Expression. It looks like:
new java.text.SimpleDateFormat("MMMMM").format(new Date())
The issue comes in with the other months. I initially tried
new java.text.SimpleDateFormat("MMMMM").format(java.util.Calendar.getInstance().add(Calendar.MONTH, new Integer("-1)).getTime())
This does not work since Calendar.add does not return a Calendar instance. I then tried using a variable and then a combination of variables which also did not work.
How to make Jasper Reports programmatically determine the Name of Columns within the report it self?

I think the best approach to solving this problem is to use Commons Lang. That package provides utilities to make calculations like this very easy. By adding one extra jar you can then use expressions like this:
DateUtils.addMonths(new Date(),-1)
I find that easier to maintain than first creating a helper Calendar class and then using the ternary operator but ignoring its results.
$P{cal}.add(Calendar.MONTH, -1)
? null : $P{cal}.getTime()
If you ever need to generalize the solution then it's a lot easier to get a Date back from a SQL query than to get a Calendar. So you can quickly change to "DateUtils.addMonths($F{MyDate},-1)". Initializing a Calendar to match a date returned by a query isn't nearly as simple. But of course there's a certain benefit to not having to add one more .jar file. So sometimes that ternary operator technique is the quickest way to get things done.
I wrote about using the Commons Lang approach a couple of years ago here: http://mdahlman.wordpress.com/2009/09/09/jasperreports-first-dates/

I also needed a certain format for the previous month. I ended up combining the other two answers:
new java.text.SimpleDateFormat("yyyyMM").format(
org.apache.commons.lang3.time.DateUtils.addMonths(
new java.util.Date(),
-6
)
)
This way I don't need to add another parameter. Adding the Commons Lang jar is a non issue for me since JasperServer 5.5 comes with version 3.0 out of the box.
I hope this helps someone who stumples upon this page, just like I did.

I found a working solution that is pretty ingenious (no I did not come up with it). I found it here. The gist of it is create a parameter called call, with a default value of:
Calendar.getInstance()
and un-check the option 'Use as a prompt'. Then in your text field expression you would do:
new java.text.SimpleDateFormat("MMMMM").format(
(
$P{cal}.add(Calendar.MONTH, -1)
? null : $P{cal}.getTime()
)
)
What happens is it will set the default value for the calendar instance, then execute the add method, which will resolve to false, so then it will then return the result from getTime() method which gets formatted how I want.

Related

DATE() function in PowerBI not working - does not recognize year value

Does anyone know why I am encountering this error with the most basic operation in PowerBI? After a few tries myself only ending in errors I copied the syntax straight off MS site and still......error.
I created a new table and used the following:
Table = calendar(date(2000,1,1),date(2030,12,31))
Error received:
Too few arguments were passed to the DATE function. The minimum
argument count for the function is 3.
When adding the arguments its as if the DATE function does not recognize the year value at all.
Anyone else encountering this?
Also, I noticed a lot of search results advising to use the "Modelling" tab which I don't have in my desktop version. I am currently still on a free trial.
Does a free trial mean I can not use the most basic DATE() function - I truly hope this is not the case.

JasperReports fails to render a Time Series Chart with error "null 'period' argument"

I have a relatively simple query that returns 3 fields of data per row, with a date. Something like this:
SELECT DATE(date) AS date, val1, val2
FROM tables
WHERE ...
From the report itself (detail band), I can confirm that all the "date" fields are non-NULL. The settings for my Time Series Chart are as follows:
TimePeriod: Day
Time Period Expression: $F{date}
(Series) Value $F{val2} - $F{val2}
The error I get is:
java.lang.IllegalArgumentException: Null 'period' argument.
at org.jfree.chart.util.ParamChecks.nullNotPermitted(ParamChecks.java:65)
at org.jfree.data.time.TimeSeriesDataItem.<init>(TimeSeriesDataItem.java:99)
at org.jfree.data.time.TimeSeries.addOrUpdate(TimeSeries.java:887)
...
I've even tried setting the "Time Period Expression" to the expression (null == $F{date} ? "foo" : $F{date}) just to see if I can get it to fail in a different say (like ClassCastException), but it still fails with the same message.
The data type for $F{date} is java.sql.Date, and the data type for the other two fields is java.lang.Long for both.
I'm using Jasper Studio 6.4.0 if that makes any difference.
I may have found an answer, and it looks like it might be a bug in JasperReports.
I searched the web for answers for this question before posting, and most questions went unanswered or responses were along the lines of "happening to me, too" or "works for me" without any real answer.
After making arrangements for every single possible null value to be handled, it still didn't work. So I tried changing the "TimePeriod" setting to "" just to see what would happen. No change, but going back to edit the chart, I noticed that "Day" was still the setting for the "TimePeriod" parameter. Weird.
I really want day-level reports, but I was grasping at straws, so I changed the TimePeriod to "Week", and the report ran successfully.
Then, I changed the "TimePeriod" from "Week" back to "Day" and it's running without error. So perhaps the default "TimePeriod" setting is in fact <NULL> but Jasper Studio shows that as "Day" when it's really not? I dunno. But try changing the TimePeriod to something else, then to what you really want.

Google spreadsheet month name (MMMM) without declension

I want to display formatted month name in the basic form (nominative) as a label. In Czech language (and several next Slavic languages) we use declension. So if I use =TEXT(NOW();"MMMM") the cell shows month name in genetive instead of nominative (i.e. srpna instead of srpen).
Q: How to format the date in the nominative? The acceptable solution will offer some native way how to solve it but not:
manipulating with strings (month names are too complex)
having some another list with month names
calling own formula (Google script)
Maybe there is no native way how to solve this and it will be implemented in the future, so Google Script seems to be the easiest hotfix for now. And because I'm expecting many answers with the script, I'm putting the one here but this question is not about the javascript/google-script.
function monthName(date) {
var months = ["Leden","Únor","Březen","Duben","Květen", "Červen","Červenec","Srpen","Září","Říjen","Listopad","Prosinec"];
return months[date.getMonth()];
}

In Jaspersoft Studio, how do I use the DATEFORMAT() function?

Here's a screenshot:
Here's a screenshot http://www.coletrumbo.com/wp-content/uploads/2015/05/dateformat-1024x575.png
I'm trying to turn the current date into July 1st of the current year using DATEFORMAT(). I learned how to do that in MySQL from this question, and I hoped it would work similarly in Jaspersoft Studio- turns out date_format( curdate(), '%Y-07-01' ) doesn't translate into
DATEFORMAT( TODAY(), '%Y-07-01' ) or DATEFORMAT( TODAY(), YY/07/01 ). Neither worked.
I could keep trying to get creative and hopefully find something that works, but I'd rather actually understand how to use DATEFORMAT().
I checked the Jaspersoft Studio User Guide, but it's not there. From the prompts on the screen, it makes a lot of sense, but I just can't figure out the "format pattern" that I'm allowed to apply, or even how to correctly write any format pattern at all. Also, this conveniently named question, DateFormat Pattern, didn't actually help at all. And community.jaspersoft.com/answers is kind of a joke in my opinion. When I checked it a couple days ago, it was filled with spam linking to live hockey games.
Thanks in advance. I'm sure this is a beginner level question, so I feel dumb asking it, and I feel like I'm wasting other people's space and time with it because I should already know. So I really appreciate your willingness to care.
I am using the following to get the todays date in a danish format
"Dato: "+new java.text.SimpleDateFormat("dd MMMM yyyy",new
Locale("da", "DK")).format(new Date())
you can find the source code for the DateTime functions directly in JR repository: https://sourceforge.net/p/jasperreports/code/ci/master/tree/jasperreports/demo/samples/functions/src/net/sf/jasperreports/functions/standard/DateTimeFunctions.java
As you can see the code is fairly simple and relies on the Joda Time library.
Therefore the second parameter you are trying to enter is a String, while the first one is a Date object.
Indeed something that could work for you is an expression like this DATEFORMAT(TODAY(), "07-01-YYYY")
Regards,
Massimo.

How do I validate dates in a Perl CGI-script?

I'm creating a small reporting script in Perl CGI. I want to have the start/end date there to filter the events on the report. So now I wonder how to validate the input in some easy, Perl-way.
The way I generate the form with those fields is:
print textfield({-name=>'start_date', -value=>$start_date});
print textfield({-name=>'end_date', -value=>$end_date});
Then it goes to the database query.
Is there a simple, Perl-ish way to validate those dates? Not only as having the right number of characters, as this is simple enough via a regexp, but I'd like to report some error if the user enters 29.02.1999 or so.
I'll just go ahead and own up to being crazy, but what I like to do is use Time::Local, and flip the time back to epoch time, and then, when it's a nice clean integer, you can impose whatever sort of sanity check you like on it.
For general form validation you should use a nice framework like Data::FormValidator which has a Data::FormValidator::Constraints::DateTime module for date validation
Disclosure: I'm the author of Data::FormValidator::Constraints::DateTime
FormFu seems to be the popular library for handling forms in Perl these days. I believe it replaces the CGI HTML generation code, but it is quite powerful.
It includes a bunch of code for validating common data types, including dates.
You can calculate dates in Perl with Time::Local, which can be set to accept invalid times and still calculate the correct date (e.g. the 32th day of a month) or the check the dates and reject invalid dates.
I nearly always opt to convert something like this into DateTime object. And I use something like DateTimeX::Easy to help ease the process:
use DateTimeX::Easy;
my $start_date = DateTimeX::Easy->new( '30th Oct 2009' );
my $from_date = DateTimeX::Easy->new( 'today' );
say $start_date->dmy;
say $from_date->dmy;
# => 30-10-2009
# => 01-10-2009
The only caveat is that DateTime and its associated modules are a bit hefty on memory which is something you may want to keep an eye on when using it in CGI.
/I3az/