soham Posted July 18, 2007 Share Posted July 18, 2007 Hello, I have a Field in my report of type 'java.util.Date'. I need to display the date in the respective timezone and locale of the user. I pass the timezone and locale of the user through the parameters REPORT_TIME_ZONE and REPORT_LOCALE. If I have data for the date field, jasper displays the date in the respective timezone and locale. But, when there is no data, i need to display a string such as 'N/A' which I cannot do since the field is of "java.util.Date" type. One partial solution would be.:)1. Change the field type to String.2. Create a parameter with name 'DateFormat' of type 'java.text.DateFormat' with default expression as "new java.text.SimpleDateFormat("HH: mm : ss a, yyyy z")"3. Now use the above parameter to display the date as string. $P{DateFormat}.format($F{longvalueofthedate} With this solution i can display the date to the user in his timezone. But, not in his locale since I am specifying a pattern in the step 2. Any help from jasper dudes?:( Link to comment Share on other sites More sharing options...
lucianc Posted July 18, 2007 Share Posted July 18, 2007 There are several way to achieve this. These are two:If you have JR >= 1.2.8, you can obtain a date formatter and use it in the text field:Code:<parameter name="DateFormatter" class="java.text.DateFormat" isForPrompting="false"> <defaultValueExpression>$P{REPORT_FORMAT_FACTORY}.createDateFormat("yyyy-MM-dd", $P{REPORT_LOCALE}, $P{REPORT_TIME_ZONE})</defaultValueExpression></parameter>..<textFieldExpression>$F{date} == null ? "N/A" : $P{DateFormatter}.format($F{date})</textFieldExpression>You can create two text fields positioned at the same coordinates, one displaying the date and the other "N/A", and switch between them using <printWhenExpression>[/ol] HTH,Lucian Link to comment Share on other sites More sharing options...
soham Posted July 19, 2007 Author Share Posted July 19, 2007 Post edited by: soham, at: 2007/07/19 15:25 Link to comment Share on other sites More sharing options...
soham Posted July 19, 2007 Author Share Posted July 19, 2007 Hello Lucian. Appreciate your quick response. Sorry, for the late reply though. I have been trying both the methods. I still did not figure out proper way to use the printwhenExpression in my case. The other method worked in the same way which i mentioned earlier. I cannot display the locale of the user. Some countries display the Date first and some countries display the month first in their locale. If i specify the pattern "yyyy-MM-dd", it will always display the date in that format no matter whatever is the user's locale. In short, i do not want to specify pattern anywhere. Since it might change from country to country. In US, 7/19/07 10:20AMIn England, 19/7/07 04:20PM Thank You,Naveen Link to comment Share on other sites More sharing options...
lucianc Posted July 19, 2007 Share Posted July 19, 2007 Using printWhenExpressions:Code:<textField> <reportElement ..> <printWhenExpression>Boolean.valueOf($F{date} != null)</printWhenExpression> </reportElement> <textFieldExpression class="java.util.Date">$F{date}</textFieldExpression></textField><staticText> <reportElement ..same position as above..> <printWhenExpression>Boolean.valueOf($F{date} == null)</printWhenExpression> </reportElement> <text>N/A</text></staticText> Using the date formatter, you can use a "style" pattern: for instance, the "medium,medium" pattern will produce a date/time format with java.text.DateFormat.MEDIUM style (for both date and time). HTH,Lucian Link to comment Share on other sites More sharing options...
soham Posted July 20, 2007 Author Share Posted July 20, 2007 That worked perfect!!!:cheer: I used the printWhenExpression. Thank you very much lucian Cheers,Naveen Link to comment Share on other sites More sharing options...
vk01 Posted December 5, 2007 Share Posted December 5, 2007 Hi, We are using a java servlet to set and pass all the report parameters. We are trying to set the user TIME_ZONE as well, trying to set it as java.util.Timezone datatype - but when trying to equate to the database value - since the database value is stored as a string i.e. "America/New_York" - it is not working comes back with error message, since database value is a string and REPORT_TIME_ZONE is a java.util.timezone type. Please have any suggestions? Link to comment Share on other sites More sharing options...
biztux Posted March 2, 2010 Share Posted March 2, 2010 We experienced the following problem:Our Servers are configured in The timezone Australia/Melbourne (+10:00/+11:00 DST).Whenever we used java.text.SimpleDateFormat("yyyy-MM-dd HH:mm").format($P{StartDate}) in reports the output was an Additional 10 or 11 (Dependant upon DST or not) hours in front of what it should display. Solution:At the top of the jrxml file where parameters are created we used the following:<code> <parameter name="StartDate" class="java.sql.Timestamp"> <parameterDescription><![CDATA[Report Start Date]]></parameterDescription> </parameter> <parameter name="EndDate" class="java.sql.Timestamp"> <parameterDescription><![CDATA[Report End Date]]></parameterDescription> </parameter> <parameter name="DateFormatter" class="java.text.DateFormat"> <defaultValueExpression><![CDATA[$P{REPORT_FORMAT_FACTORY}.createDateFormat("yyyy-MM-dd HH:mm", $P{REPORT_LOCALE}, java.util.TimeZone.getTimeZone("GMT"))]]></defaultValueExpression> </parameter></code>In the Page Footer section where we wanted to display the Start and End dates for the Report:<code> <textField> <reportElement x="180" y="7" width="377" height="13"/> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"><![CDATA["Date range between: "+$P{DateFormatter}.format($P{StartDate})+" and "+$P{DateFormatter}.format($P{EndDate})]]></textFieldExpression> </textField></code> The results are that we now have correct values which are only dependent upon the User's Time Zone settings...Code:<!-- PARAMETER DECLARATIONS --> <parameter name="StartDate" class="java.sql.Timestamp"> <parameterDescription><![CDATA[Report Start Date]]></parameterDescription> </parameter> <parameter name="EndDate" class="java.sql.Timestamp"> <parameterDescription><![CDATA[Report End Date]]></parameterDescription> </parameter> <parameter name="DateFormatter" class="java.text.DateFormat"> <defaultValueExpression><![CDATA[$P{REPORT_FORMAT_FACTORY}.createDateFormat("yyyy-MM-dd HH:mm", $P{REPORT_LOCALE}, java.util.TimeZone.getTimeZone("GMT"))]]></defaultValueExpression> </parameter><!-- REPORT PAGE FOOTER SECTION --> <textField> <reportElement x="180" y="7" width="377" height="13"/> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"><![CDATA["Date range between: "+$P{DateFormatter}.format($P{StartDate})+" and "+$P{DateFormatter}.format($P{EndDate})]]></textFieldExpression> </textField> Link to comment Share on other sites More sharing options...
thangalin Posted April 25, 2016 Share Posted April 25, 2016 See: http://community.jaspersoft.com/questions/992316/canonical-step-step-guide-global-date-format-jaspersoft-studio-6x Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now