Jump to content
We've recently updated our Privacy Statement, available here ×

How to embedd more java functions at the expression box


adiboing

Recommended Posts

Hey guys :)

I have a little question.

When I have a Variable lets say $V{Integer} and a Field $F{String}. The names are also their datatypes. ;)

And now when I would like to convert from the $F{String} which only includes numbers into $V{Integer} I don't now how.

When I try some of the expressions which I found here in the forum I just recive errors.

Integer.praseInt($F{String})

Integer.intValue($F{String})

Integer.toInteger($F{String})

And when I press CTRL + Blankspace at the dot  I can't find any of these functions or even in the right area where I marked the red arrow. They aren't there, and I just get an Error (also in the picture) :(

http://img43.imageshack.us/img43/7729/functions.png

 

So can anybody tell me what I have to do to include for example those java functions that I'm able to select one of those? And additional how can I use the java.text.SimpleDateFormat?

I use iReport 4.0.2 and Groovy

Somehow it has to be able because I wrote alot about people who use one of these functions :D

peace Adi ;)


 

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

The error is because you wrote "Integer.praseInt" instead of "Integer.parseInt".

The list of functions on the right includes a list of useful functions based on the datatype of the Field or Variable you have selected. It doesn't include static functions of other classes like java.lang.Integer. I'm not exactly how it could do that... but it seems like there's a valid enhancement request in there somewhere. Please log it in the tracker and add as much detail as you can about what the feature should include.

If you have commonly used functions, you can add them to the "User Defined Expressions" that you see in the left panel. Go to Tools -> Options and you'll see the area you can define these. For example, you might want to add "Integer.parseInt(String)" so that you can just pick it from the list and not risk entering a typo. ;-)

Regards,
Matt

Link to comment
Share on other sites

Hey mdahlman :)

 

When I look into the properties of the Variable pureInt I can see the functions parseInt(String), intValue() and decode(String), getInteger(String). All I changed was that I started my expression with the Variablename $V{pureInt} and not with Integer.parseInt(...).  But still none of these functions is working. Everyone prints a "null".

$V{pureInt}.parseInt($V{blanknums})

$V{pureInt}.intValue($V{blanknums})

$V{pureInt}.getInteger($V{blanknums})

$V{pureInt}.decode($V{blanknums})

and not even when I try this:

new Integer($V{pureInt}.parseInt($V{blanknums}))

or

new java.lang.Integer($V{pureInt}.parseInt($V{blanknums}))

 

all I get is "null"....

http://img171.imageshack.us/img171/3928/stringint.png

 

My real goal to achive is actually to convert a field from type string which includes a date (yyyy-MM-dd) to have it in a varibale form the datatype date. Thats why I also asked about this SimpleDateFromat thing.

With this conversion from string to int I just searched a way to get this integer into a date... because I'm not able with a string to get it as an date...

 

and not even this parseInt() is working... what is wrong with my iReports :(?

 

please help, Adi

 

Link to comment
Share on other sites

Your question is really more of a Java question than an iReport question. It's fine to post questions like that here... but you might get better responses on a Java forum somewhere.

This expression is valid... but it's not good:

$V{pureInt}.parseInt($V{blanknums})

The reason I say it's not good is because it's very misleading. It is exactly identical to this:

java.lang.Integer.parseInt($V{blanknums})

That's because parseInt is a static method on the Integer class. It can be very useful... but it completely ignores the class used to call it. In your example the variable $V{blanknums} gets parsed and purInt gets ignored. That's not at all what you're attempting to do.

SimpleDateFormat is great for parsing a Date into a String:

 

new SimpleDateFormat("yyyyMMdd000000").format($P{StartDate}) 
new SimpleDateFormat("dd-MMM-yyyy").format($P{StartDate})

But you want the opposite of that. I don't think SimpleDateFormat will help you. I think you need to create (or find) a different helper class. Java Common Lang has the function "parseDate" to do exactly what you're looking for:

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/DateUtils.html

I wrote an article back in 2009 explaining how to use the Commons Lang classes in a report. Date Calculations in JasperReports. It doesn't explicitly show converting Strings to Dates. But you should be able to first follow the attached example there, and then you can add a Field or a Variable that uses org.apache.commons.lang.time.DateUtils.parseDate.

Let us know how it goes.

Regards,
Matt

P.S. If your data is coming from a database, then it's probably easier to parse your strings into date objects inside the SQL query.

 

 

Link to comment
Share on other sites

Wow this DateUtils-Class looks very interesting :D, seems like thats what I need somehow thank you matt ;)

 

I recive my data from an excel datasource so I'm not able to define any sql query :( because of that I have so many problems.

On your page you talked something about a package?

"Don’t forget that you need to import the relevant package. In iReport click on the root report element, and in its properties you’ll find Imports. Add “org.apache.commons.lang.time.*” (without the quotes) as an import."

I'm not really used to this java stuff. But I think I have to get something like a .jar file or something which contains this DateUtils-Class.

Cause it doesn't seems like my iReports knows this DateUtils yet. Its the same with this SimpleDateFormat.

 

I tried your stuff from your webpage and it didn't worked and I thought that it can't work until you "teach" iReports how to use DateUtils. So I searched for a .jar file in the internet and found this:

http://findjar.com/jar/commons-lang/jars/commons-lang-2.3.jar.html

I downloaded it and added the jar under the options in iReports. Okay now your example.jrxml is working but not all expressions. The "getFragment..." and the "setDay..." functions are not working and are throwing errors. So I deleted those till I was able to compile your example.

So I played a bit with your expressions and then tried to use the parseDate() function which I need. But somehow its not working as the 2 functions I told you above :(

I have added a second Parameter called: $P{stringDate} - datatype: String
and a Variable called: $V{getDate} - datatype: Date

My expression for the variable getDate is the following:
DateUtils.parseDate($P{stringDate}, "dd-MM-yyyy")

and the parseDate function should look like this:
parseDate(java.lang.String str, java.lang.String[] parsePatterns)

My problem seems to be that I'm not using a String[]

Here is the error I recive:
1. The method parseDate(String, String[]) in the type DateUtils is not applicable for the arguments (String, String)
                value = (java.util.Date)(DateUtils.parseDate(((java.lang.String)parameter_stringDate.getValue()), "dd-MM-yyyy")); //$JR_EXPR_ID=9$                                                    <------->

And now I don't now how to go on :( could you give me an example? Cause it seems that this is the only function to achieve a Date out of a String =/

 

Hope you can help me again :D

Looking forward to your answer. Greetz Adi ;)

 

PS: All I need is a Variable (Type: Date) with a date from a field (Type: String)

When my Parameter is set to 01/01/0001 I can use the following to change this date into 01/01/2001, but the expression only works in a textfield and not in a variable what doesn't makes any sense to me. Why textfield and not variable, too?

DateUtils.addYears($P{MyDate},(2001-1))



Post Edited by adiboing at 07/25/2011 10:12
Link to comment
Share on other sites

I don't really know Java all that well... but you need something sort of like this:

DateUtils.parseDate($P{stringDate}, {"dd-MM-yyyy"} )

They made it an array so that you have the flexibility to define several patterns to try:

 

DateUtils.parseDate($P{stringDate}, {"dd-MM-yyyy", "dd/MM/yy"} )

 

Google up how to create a String array if that doesn't work. And please post the answer. 

In contrast, there should have been no need to Google for the Commons Lang jar. My article links straight to the Commons project. You can download the binaries there: http://commons.apache.org/lang/download_lang.cgi.

Good luck,
Matt

 

Link to comment
Share on other sites

Yeay I solved it /tools/fckeditor/editor/images/smiley/msn/teeth_smile.gif

biggg thanks to you matt!

In the end I wasn't able to tell the expression an array of this dateformats which worked. I solved my issue on a bit different way.

 

For every single iReport user.
Cast a String to Date worked in my case like this:

 

My Datasource was an Excelfile, all fields were from type String.

I have a field with dates in formatting yyyy-MM-dd-HH-mm it is called:
$F{Bestelleingang} - (String)
To work well with the field I had to convert it into a date within a Variable, this variable is called:
$V{dateBestelleing} - (Date)

 

And now the expression to cast my string from the field into a valid date is following:

DateUtils.parseDate((new java.lang.String($F{Bestelleingang}.length() > 10 ? $F{Bestelleingang}.substring(8,10) + "/" + $F{Bestelleingang}.substring(5,7) + "/" + $F{Bestelleingang}.substring(0,4) : "01/01/0001")), "dd/MM/yyyy")

Out of: yyyy-MM-dd-HH-mm
will be: dd/MM/yyyy

 

I know my expression seems kinda wirred and long but its the only way I found. Before I was able to use the DateUtils.parseDate() function I had to add the jar-file from matts website ;)
(http://commons.apache.org/lang/download_lang.cgi) Take the commons-lang3-3.0-bin.zip in the category Binaries. 

And to include it to iReports (I use iReports 4.0.2) go to the Tools -> Options -> Classpath and then Add JAR

Don't forget to rightclick into your Report Inspector on your project and go to Properties -> Imports -> include "org.apache.commons.lang3.time.*" (without the quotes)
Now you don't have to start your expression with org.apache.commons.lang3.time.DateUtils.parseDate(...

 

If everything workedout you should have a valid date fromated value in a Variable ;)

If it was helpfull to someone please give me a karma-point :)

peaceout Adi :D

 

 



Post Edited by adiboing at 07/28/2011 09:07



Post Edited by adiboing at 07/28/2011 09:08
Link to comment
Share on other sites

  • 2 years later...

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...