Jump to content

Need help with formatting date fields in an expression


jkeri
Go to solution Solved by prusyn,

Recommended Posts

IN my query I have a date field $F{DATE1} as class java.util.Date

What I want to do is to print  and concatenate $F{DATE1} and $F{DATE1} + 6 days

I want my result like this:     01/01/2019 | 01/07/2019 

My expression is:

new SimpleDateFormat("MM/dd/yyyy").format($F{DATE1})+"|"+new SimpleDateFormat("MM/dd/yyyy").format($F{DATE1} +6)

What is the correct exression to accomplish this?

 

Thank you in advance.

 

 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution

Hi,

I had to play around a bit, but was able to achieve your desired result. I have created parameter:

    <parameter name="cal" class="java.util.Calendar" isForPrompting="false">        <defaultValueExpression><![CDATA[Calendar.getInstance()]]></defaultValueExpression>    </parameter>[/code]

and variable:

<variable name="newDate" class="java.util.Date"><variableExpression>    <![CDATA[        (        $P{cal}.set($F{birthdate}.getYear()+1900, $F{birthdate}.getMonth(), $F{birthdate}.getDate()) ||        $P{cal}.add(Calendar.DATE, 6)        )        ? null : $P{cal}.getTime()     ]]></variableExpression></variable>[/code]

Result looks like that: http://prntscr.com/m8xtdz

I'll attach sample report with sample data. You should be able to run it without any issues. Let me know if this fits your requirements :)

Link to comment
Share on other sites

Hi,

You can to use just java 8. 

Your expression is:

new SimpleDateFormat("MM/dd/yyyy").format($F{DATE1})+"|"+new java.sql.Date($F{DATE1}.getTime()).toLocalDate().plusDays(6).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))

Link to comment
Share on other sites

Hi,

You can to use just java 8. 

Your expression is:

new SimpleDateFormat("MM/dd/yyyy").format($F{DATE1})+"|"+new java.sql.Date($F{DATE1}.getTime()).toLocalDate().plusDays(6).format(java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"))

Link to comment
Share on other sites

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