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

Date field with timezone, locale as N/A


soham

Recommended Posts

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

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

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

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:20AM

In England, 19/7/07 04:20PM

 

Thank You,

Naveen

Link to comment
Share on other sites

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

  • 4 months later...

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

  • 2 years later...

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

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