How can i convert 24 format time to 12 format in jasper studio via expressions?
i have string like below
"19:00" and i want to convert it to "07:00 PM"
Do the same as in java see DateTimeFormatter as you can see the correct format is hh:mm a
The best solution in jasper-reports is using pattern attribute on the textField tag since it will conserve the time object if you export to for example excel
<textField pattern="hh:mm a">
<reportElement x="0" y="0" width="100" height="20"uuid="b8baea82-84c4-42fa-bccd-62abc96eeded"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{theTime}]]></textFieldExpression>
</textField>
Another solution is using the SimpleDateFormat in textFieldExpression
<textFieldExpression><![CDATA[new SimpleDateFormat("hh:mm a").format($F{theTime})]]></textFieldExpression>
NOTE: $F{theTime} needs to be of the class java.util.Date or one of its extensions java.sql.TimeStamp,java.sql.Date,java.sql.Time
Use the Pattern property..
Click on the Text Field you want to format.
Right click, click Show Properties.
Click on the Text Field tab.
Look for Pattern and click on the square with the 3 dots (...).
Choose the type of pattern you want to format your Text Field to. You can type a new format yourself if you are not satisfied with the pre-defined ones.
Same as above can be achieved by going to Advanced Properties and looking for the Pattern property.
Hope it helped. Cheers :)
Related
How to give space after every character of a string in jasper reports. I need this functionality to print date according to cheque format. I am new to jasper. Please help me in this.
Adding blank to every character
unfortunately Jasperreports doesn't support letter spacing (source)
However you can always use java in text fields to tranform the string and add a blank character after each character.
For example in case you have a field myString (source for java part):
<textFieldExpression>
<![CDATA[$F{myString}.replace("", " ").trim()]]>
</textFieldExpression>
More specifically on dates
Java also provides nice feature to format a date. You might consider this as you specified you were working with dates.
An example of date formatting :
<variable name="dateFormatter" class="java.text.SimpleDateFormat">
<variableExpression><![CDATA[new java.text.SimpleDateFormat("yyyy-MM-dd")]]></variableExpression>
</variable>
<textFieldExpression>
<![CDATA[$V{dateFormatter}.format($F{myDate})]]>
</textFieldExpression>
See full documentation here
replaceAll() works fine in jasper reports only if RegularExpression is added in the imports.
value="com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression"/>
$P{mystring}.replaceAll("", " ").trim()
I have four reports that are basically different formats of the same report and the following problem happens with three of them. I have a text field in the Detail's band that is marked as Stretch with Overflow. The problem is that it seems that it has some kind of characters limit, because with the following text (110 characters):
MAD.ESP.TAUARI(COURATARI GUIANENSIS AUBL.)SERRADA EM RIPA.KD.FAS.FSC 100% MED.19,1MMX38,1MMX2438,4MM DE COMP.
It's only printing:
MAD.ESP.TAUARI(COURATARI GUIANENSIS AUBL.)SERRADA EM RIPA.KD.FAS.FSC 100% MED.19,1MMX38,1MMX2438,4MM DE
As shown in the screen shots:
It's woth noting that when using the "preview" of the JasperStudio, the whole text appears normally for all of them.
Here's the text field's code of one of them. They're basically the same, changing the width, height and position.
<textField isStretchWithOverflow="true">
<reportElement x="26" y="0" width="76" height="10">
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Top">
<font fontName="Arial" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$F{descricaoProduto}]]></textFieldExpression>
</textField>
I'm using the 6.2.2 version of JasperReports.
EDIT:
I created a gist with a minimal version of the report that the problem happens:
Sample
I'm exporting it to PDF and using Arial as the font (I added a JAR with the Arial font).
Set the net.sf.jasperreports.export.pdf.force.linebreak.policy property to true (in jasperreports.properties or programmatically). See http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.export.pdf.force.linebreak.policy
The property instructs the PDF exporter to use java.text.BreakIterator to determine where line breaks can occur in texts, which ensures that the line breaks at PDF export time match the breaks uses when the text was measured at report fill time. As a note, this might come with some impact on PDF exporting performance, but it shouldn't be dramatic.
I'm having a problem with my PDF report where a String in a text field is truncated before filling the text field. The amount of missing characters (5-6) would not be enough to go over the end of the textField.
I added the text.truncate.at.char property to the report element but the String is still truncated (after displaying some more characters than before).
I also checked if there are other report elements blocking the end of the text field, but there are none.
Lastly I tried a potential fix I found and added "\n" to the end of the line, but that also did not help.
In iReport Designer the whole String is displayed.
PDF report
iReport Designer Preview
Does anyone know how to make Jasper Reports use the whole space in the text field?
PS: Allowing the text to overflow to the next line is not possible due to customer wishes.
I'm using Jasper Reports 5.5.2.
Edit:
The textField is part of a detail band in a subreport. I'm not allowed to share the whole .jrxml, but this is the code for the textField:
<textField>
<reportElement style="Unicode" mode="Opaque" x="0" y="2" width="467" height="17" forecolor="#FFFFFF" backcolor="#00007F" uuid="e810d7a4-6802-4620-af2f-4c385a9e80a6">
<property name="net.sf.jasperreports.text.truncate.at.char" value="true"/>
</reportElement>
<textElement verticalAlignment="Middle" markup="none">
<font size="10" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[" More Details - "+$F{Description} + " ("+$F{Id}+")"]]></textFieldExpression>
</textField>
The description is of variable length, the Id is a GUID. In this case there should still be more than enough space in the textField to display the whole GUID.
Here is another screenshot with isStretchWithOverflow="true":
The text is now displayed completely in the first line but the textField is larger which is not accepted by the customer.
Why is it different in pdf and iReport designer?
This is because iText (the library creating your pdf) is doing its "best effort" to render the font you have indicated in jrxml and its not good enough (it is using another font that is bigger...).
To avoid these problems you need to use font extensions and check your settings on style and textElement
Checklist to rendered font correctly in pdf
How to add font extension using iReport
OP solved as in comment:
Font extension installed, removing isBold="true" on textElement, since the style set to textElement had isBold="false"
i am print a value in jasper report but it's not display decimal precision.
but display value this type 20.00000000
and i need a Only "20.00" decimal precision
how to solve it
please replay
You can try the below expression
new DecimalFormat("#,##0.00").format($F{FieldValue})
Hope this should solve your problem
Try with this entry into jasper file:
<textField pattern="###0.00;(###0.00)">
<textFieldExpression class="java.lang.String"><![CDATA[$P{percentage_rate}]]></textFieldExpression>
</textField>
Second approach is via java code: Convert the value in 2 decimal place in java file that will be fetched by jasper file.
String percentage_rate = new BigDecimal(p_rate!=null ?p_rate:"0").setScale(2, RoundingMode.HALF_UP).toString();
you can try using field pattern property(right click on field) and use pattern style according to your requirement.
right click on field ->field pattern ->(Category)Number ->you can chose the decimal places.
using ireport IDE
Hope this helps
Use the code below on your textfield you would like to format in decimal places i.e .00 formats it into two decimal places. to add more decimal places add the number of zeros eg .000 3 decimal places, .0000 4 decimal places etc
<textField pattern="#,##0.00">
<textFieldExpression>
<![CDATA[$P{amount}]]>
</textFieldExpression>
</textField>
i created report in iReport, where is text field with format:
new DecimalFormat("#,##0.00").format($F{NUMBER}) + " €"
In iReport result is 1 234,56 €, which is OK. I am in Slovakia and this is ordinary format here.
In Jasper Server is result 1,234.56 € which is not OK.
Other question: There is still Sk (Slovak Crown) set for Slovakia. Is it possible to set it for EUR ?
Could you help me, what to set where ?
Thanks
If you hard-code the format to use comma for the thousands separator, it's not possible that iReport would ignore this. It sounds like you have not done what you think.
But it seems likely that you need to set the JasperReports Server to use a different locale. You could edit this file:
WEB-INF/applicationContext-security.xml
You'll find the locale section. Add what you need for Slovakia. Then you can choose this locale on the login page.
Or it might be helpful to set the locale in Tomcat's startup script (or whatever app server you are using). These Java parameters might solve your problem (of course you won't want en_US... but those are the parameters):
java -Duser.language=en -Duser.region=US
I have an older version of jasperserver deployed in one of my clients and upgrading it would be a bit of a pain. I'm experiencing the same thing with iReport 4.1.1. My locale is pt_BR and I didn't really want to change my server locale.
I've noticed that iReport is smart (or dumb) enough to not add the class attribute to the textFieldExpression class (I am guessing it guesses from the variable/field type). Jasperserver (at least the version I'm using anyway), on the other hand, does not apply the pattern to numbers if you don't specify the class for that expression.
In other words: iReport generates this markup and renders it as expected:
<textField pattern="###,###,##0.00" isBlankWhenNull="true">
<reportElement mode="Transparent" x="1293" y="0" width="92" height="20"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{my_var}]]></textFieldExpression>
</textField>
You can assume that my_var is a BigDecimal. This field does not get rendered correctly in the server, as it simply ignores the pattern (maybe because it assumes all fields are Strings?) unless you specify the expression class for the textFieldExpression:
<textField pattern="###,###,##0.00" isBlankWhenNull="true">
<reportElement mode="Transparent" x="1293" y="0" width="92" height="20"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression class="java.math.BigDecimal"><![CDATA[$V{my_var}]]></textFieldExpression>
</textField>
Note the class="java.math.BigDecimal" in the second last line. It sucks that I had to manually edit the jrxml file, but at least Jasperserver renders my report properly.