iReport and Jaspersoft Studio: Useful Expressions

Replace month with a roman numeral

If you want to replace the month with a roman numeral in iReport, use the following expression:

"Date: "+($F{Start_Date_(Day)}==null?"":$F{Start_Date_(Day)}+".").toString()+ ($F{Start_Date_(Month)}+".").toString().replace("10","X").replace("11","XI").replace("12","XII").replace("1","I").replace("2","II").replace("3","III").replace("4","IV").replace("5","V").replace("6","VI").replace("7","VII").replace("8","VIII").replace("9","IX")+($F{Start_Date_(Year)}==null?"":$F{Start_Date_(Year)}).toString()

Making dates display full month names instead of numbers

($F{Collection_Date_(Day)}==null?"":$F{Collection_Date_(Day)}+"
").toString()+($F{Collection_Date_(Month)}==null?"":$F{Collection_Date_(Month)}+"
").toString().replace("10","October").replace("11","November").replace("12","December").replace
("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May
").replace("6","June").replace("7","July").replace("8","August").replace("9","September")
+($F{Collection_Date_(Year)}==null?"":$F{Collection_Date_(Year)}+"").toString()

Making dates have extra zeros

For example, you date looks like this 2012/7/9 and you want it to look like 2012/07/09. Note that
the format is year/month/day here, you can switch it around as you wish. Use this expression:

"Date: " + ($F{Date_(Year)}==null?"":$F{Date_(Year)}+"").toString()
+($F{Date_(Month)}==null?"":"/"+(($F{Date_(Month)}+"").toString().length() == 2 ?
($F{Date_(Month)}+"").toString(): "0" + $F{Date_(Month)}+"")).toString() +
($F{Date_(Day)}==null?"":"/" + (($F{Date_(Day)}+"").toString().length() == 2 ?
($F{Date_(Day)}+"").toString(): "0" + $F{Date_(Day)}+"")).toString()

Display date as “Month Day, Year”

This would display as something like November 19, 2000

($F{Date_(Month)}==null?"":$F{Date_(Month)}+" ").toString().replace("10","October").replace("11","November").replace("12","December").replace("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May").replace("6","June").replace("7","July").replace("8","August").replace("9","September")+($F{Date_(Day)}==null?"":$F{Date_(Day)}+", ").toString()+($F{Date_(Year)}==null?"":$F{Date_(Year)}+" ").toString()

Getting rid of extra punctuation when fields are null

For example, you don’t want a report to look like ‘South America, Paraguay , , , ‘ if the rest of
some of the fields in the middle of the expression are null. To only make extra punctuation or
even words show up only when there is information in the field, format like this:

($F{Continent}==null?"":$F{Continent})+($F{Country}==null?"":", " +
$F{Country})+($F{State}==null?"":", " + $F{State})+($F{County}==null?"":", " +
$F{County})+($F{Locality_Name}==null?"":", " + $F{Locality_Name})+($F{Latitude1}==null?"":",
" + $F{Latitude1}+"°").toString()+($F{Longitude1}==null?"":", " + $F{Longitude1}+"°").toString()
Insert your own fields or do whatever you want, but this should work perfectly.

Jaspersoft Studio

Cataloged Date display full month names instead of numbers

($F{1.collectionobject.catalogedDateNumericDay}==null?"":$F{1.collectionobject.catalogedDateNumericDay}+"
").toString()+($F{1.collectionobject.catalogedDateNumericMonth}==null?"":$F{1.collectionobject.catalogedDateNumericMonth}+"
").toString().replace("10","October").replace("11","November").replace("12","December").replace
("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May
").replace("6","June").replace("7","July").replace("8","August").replace("9","September")
+($F{1.collectionobject.catalogedDateNumericYear}==null?"":$F{1.collectionobject.catalogedDateNumericYear}+"").toString()

Determined Date display full month names instead of numbers

($F{1,9-determinations.determination.determinedDateNumericDay}==null?"":$F{1,9-determinations.determination.determinedDateNumericDay}+" ").toString()+($F{1,9-determinations.determination.determinedDateNumericMonth}==null?"":$F{1,9-determinations.determination.determinedDateNumericMonth}+" ").toString().replace("10","October").replace("11","November").replace("12","December").replace
("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May").replace("6","June").replace("7","July").replace("8","August").replace("9","September")
+($F{1,9-determinations.determination.determinedDateNumericYear}==null?"":""+$F{1,9-determinations.determination.determinedDateNumericYear}).toString()

Display nothing if empty or null

($F{1,5-cataloger.agent.cataloger} == null || $F{1,5-cataloger.agent.cataloger}.isEmpty() ? "" : "<br>"+"<b>"+"Cataloguer"+"</b>")

Replace value with another value

$F{1.collectionobject.text1} == null ? "" :
$F{1.collectionobject.text1}.equals("Extra Small") ? "XS" :
$F{1.collectionobject.text1}.equals("Small") ? "S" :
$F{1.collectionobject.text1}.equals("Medium") ? "M" :
$F{1.collectionobject.text1}.equals("Large") ? "L" :
$F{1.collectionobject.text1}.equals("Extra Large") ? "XL" :
$F{1.collectionobject.text1}
1 Like

Hello,

Thanks for these handy expressions.

I have the following issue though… When I use the expressions that replaces month with Roman numeral I get always values for month and day despite that for some entries I have entered only year. Should not for these cases $F{1,10.collectingevent.startDateNumericDay}==null? (and consequently, $F{1,10.collectingevent.startDateNumericMonth}==null?) be TRUE?
How to stop values entered only as Year, e.g. 2023, appear as 1-I-2023?

Hi @SPalpurina,

Specify 7 shows a value of 1 in both the numeric month and numeric day fields in query results, regardless of the value of the precision, if there is no value for the month and/or day. We have a feature request open to change this behavior: #4503

To workaround this, you can create a Jaspersoft expression that is conditional based on the value of the precision field (make sure to add this field to the query/report before proceeding):

($F{1,10.collectingevent.startDatePrecision}==3 ? 
  $F{1,10.collectingevent.startDateNumericYear}.toString() : 
  ($F{1,10.collectingevent.startDatePrecision}==2 ? 
    ($F{1,10.collectingevent.startDateNumericMonth}==null?"":$F{1,10.collectingevent.startDateNumericMonth}+" ").toString().replace("10","October").replace("11","November").replace("12","December").replace
    ("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May").replace("6","June").replace("7","July").replace("8","August").replace("9","September") + $F{1,10.collectingevent.startDateNumericYear}.toString() : 
    ($F{1,10.collectingevent.startDateNumericDay}==null?"":$F{1,10.collectingevent.startDateNumericDay}+" ").toString() + 
    ($F{1,10.collectingevent.startDateNumericMonth}==null?"":$F{1,10.collectingevent.startDateNumericMonth}+" ").toString().replace("10","October").replace("11","November").replace("12","December").replace
    ("1","January").replace("2","February").replace("3","March").replace("4","April").replace("5","May").replace("6","June").replace("7","July").replace("8","August").replace("9","September") + 
    ($F{1,10.collectingevent.startDateNumericYear}==null?"":""+$F{1,10.collectingevent.startDateNumericYear}).toString())
)

This field expression formats the start date of a collecting event based on the precision of the date information available.

  1. If the startDatePrecision is 3 (indicating a full year), it returns the startDateNumericYear as a string.
  2. If the startDatePrecision is 2 (indicating a month and year):
    • If the startDateNumericMonth is null, it returns an empty string.
    • Otherwise, it replaces the numeric month value with the corresponding month name (e.g., “10” is replaced with “October”) and concatenates it with the startDateNumericYear.
  3. If the startDatePrecision is not 3 or 2 (indicating a day, month, and year):
    • If the startDateNumericDay is null, it returns an empty string.
    • Otherwise, it concatenates the startDateNumericDay, the month name (as in the previous case), and the startDateNumericYear.

This expression replaces the month number with the name, but that is not necessary. Let me know if this solution would work for you!

1 Like

Thanks, Grant,

Thanks for your very fast solution and apologies for posting mine too late… I realized I could use the determinedDatePrecisition and collectingevent.startDatePrecisition already yesterday but I forgot to post my solution. I am glad that we arrived almost at the same solution though.

For me, the following code also worked fine. It seems that the only difference between our solutions is that the check for null in the date value is at the beginning of the whole data field. My example is with determination data but it is analogous for the starting date of the collecting event:

($F{1,9-determinations.determination.determinedDate} == null ? "" : "Det. date: " + ($F{1,9-determinations.determination.determinedDatePrecision} == 1 ? ($F{1,9-determinations.determination.determinedDateNumericDay}+".").toString() + ($F{1,9-determinations.determination.determinedDateNumericMonth}+".").toString().replace("10","X").replace("11","XI").replace("12","XII").replace("1","I").replace("2","II").replace("3","III").replace("4","IV").replace("5","V").replace("6","VI").replace("7","VII").replace("8","VIII").replace("9","IX") : ($F{1,9-determinations.determination.determinedDatePrecision} == 2 ? ($F{1,9-determinations.determination.determinedDateNumericMonth}+".").toString().replace("10","X").replace("11","XI").replace("12","XII").replace("1","I").replace("2","II").replace("3","III").replace("4","IV").replace("5","V").replace("6","VI").replace("7","VII").replace("8","VIII").replace("9","IX") : "")) + $F{1,9-determinations.determination.determinedDateNumericYear}.toString())

1 Like