Jump to content
We've recently updated our Privacy Statement, available here ×
  • Creating a "Now" function in adhoc / domain that allows you to get the current Timestamp.


    Scott Andersen
    • Features: Ad Hoc, Domains, JasperReports Server Version: v7.8 Product: JasperReports® Server

    Out of the box there is a function in the adhoc calculated field editor that allows you to get the current date.  Unfortunately this is a Date object and not a Timestamp, so you can't get done to the time level.

    You can however create a custom field "Now" that returns a Timestamp.

    1. Create the custom function in WEB-INFgroovygroovy_columnBaseGroovyColumn.groovy
        def Now() {
            def now = new Date();
            def timestamp = now.toTimestamp();
            return timestamp;
        }

    2. Add the function definition to WEB-INFapplicationContext-el-operators.xml  This defines the return data type, any input parameters, and how the function is executed.

            <!-- 'Now' function -->
            <bean parent="functionDef">
                <property name="name" value="Now"/>
                <property name="javaType" value="java.sql.Timestamp"/>
                <property name="aliases">
                    <list>
                        <value>now</value>
                    </list>
                </property>
                <property name="properties">
                    <map>
                        <!-- show in calc field dialog -->
                        <entry key="inAvailableFunctions" value="true"/>
                        <!-- always run in SQL -->
                        <entry key="alwaysInSQL" value="true"/>
                    </map>
                </property>
                <property name="inMemory" value="false"/>
            </bean>

    3. Add a function description to the property bundle for adhoc in WEB-INFbundlesadhoc_messages.properties

    adh.calculated.fields.function.description.Now=Returns Timestamp representing the current time.

    This will show up in the description in the ad hoc calculated field editior.

    The function can now  be used either in the Calucalted Field editor in the Domain Designer, or in the Ad hoc custom field editor.

    Related articles below cover the details mentioned above in greater detail.

    Adding custom fields to ad hoc

    Creating custom groovy functions 


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