Jump to content
We've recently updated our Privacy Statement, available here ×
  • Change Timezone Of Ad Hoc View Field


    akovach
    • Features: Ad Hoc Version: v7.1, v7.1.0 Product: JasperReports® Server

    Requirement

    Sometimes user needs to have Date fields represented with another Timezone. It is possible to achieve with out-of-box tools of JasperReports Server.


    Solution

    To change Timezone of a field we would need to append several configuration files with our customization.

    In BaseGroovyColumn.groovy add a new function which will convert our field with Date type to another timezone.

    def convertDateToTimestamp(Date dateField, String timezone) {
        if (dateField != null && dateField != null) {
            def date = dateField
            def timeZone = TimeZone.getTimeZone(timezone)
            def dateT = dateField.format('dd.MM.yyy HH:mm:ss', timeZone)
            def convert = new SimpleDateFormat('dd.MM.yyy HH:mm:ss').parse(dateT)
            return new Timestamp (convert.getTime())
        }
        return null           
    }
    

    In ApplicationContext-el-operators.xml file add a definition for our new function. This step is required to make sure function processes values with correct types and available in list of functions in Ad Hoc View of calculated fields.

    <bean parent="functionDef">
        <property name="name" value="convertDateToTimestamp"/>
        <property name="javaType" value="java.sql.Timestamp"/> 
        <property name="argumentTypes">
            <list>
                <value>#{objectTypeMapper.checkType('Any')}</value>
                <value>#{objectTypeMapper.checkType('Any')}</value>
            </list>
        </property>
        <property name="properties">
        <map>
            <entry key="inAvailableFunctions" value="true"/>
            <entry key="alwaysInSQL" value="false"/>
        </map>
        </property>
        <property name="inMemory" value="true"/>
    </bean>
    

    In adhoc_messages.properties add a description property for the function:

    adh.calculated.fields.function.description.convertDateToTimestamp=Convert a timezone of Date field


    Results

    In Ad Hoc View create a calculated field, which will contain function with two arguments: Date field to be converted and timezone which will be set in new field.

    timezonefunctionadhocview.png.7b9e1ddd0b5107e8759b0659b770fb23.png

    There will be a new Date field with another timezone in result.

    resulttimezoneadhocview.png.cf0cf2dd930954e9037a8bf10aefae39.png


    Useful references

    Setting behavior for functions

    Creating custom function for Ad Hoc Views


    ref case akovach 01572297 


    User Feedback

    Recommended Comments



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