Jump to content
We've recently updated our Privacy Statement, available here ×
  • Change Timestamp in Ad Hoc View Fields


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

    Requirement

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


    Solution

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

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

    def convertDateToTimestamp(Date dateField, String timezone) { // timestamp - your date field, timezone - your desired timezone
        if (dateField != null && dateField != null) {
            def date = dateField
            def timeZone = TimeZone.getTimeZone(timezone) //generate a 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 we need to 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"/> <!-- type of field returned -->
        <property name="argumentTypes">
            <list>
                <value>#{objectTypeMapper.checkType('Any')}</value> <!-- type of first argument -->
                <value>#{objectTypeMapper.checkType('String')}</value> <!-- type of second argument -->
            </list>
        </property>
        <property name="properties">
        <map>
            <!-- show in calc field dialog -->
            <entry key="inAvailableFunctions" value="true"/>
            <!-- do not use in SQL Executor -->
            <entry key="alwaysInSQL" value="false"/>
        </map>
        </property>
        <!-- use always in Ad Hoc JRS memory for calculation -->
        <property name="inMemory" value="true"/>
    </bean>
    

    Add a description property for the function in adhoc_messages.properties: adh.calculated.fields.function.description.convertDateToTimestamp=Convert a timezone of Date field

    Useful references

    Setting behavior for functions

    Creating custom function for Ad Hoc Views


    ref case akovach 01572297 


    User Feedback

    Recommended Comments

    There are no comments to display.



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