I want show my date column using the following formattings: year (2011,2012), quarter (2011-Q4,2012-Q1), month (2012-01,2012-02) and week (2012-01, 2012-02)
I can use new SimpleDateFormat($P{P_DT_FORMAT}).format($F{col_data}) for the year, month and week setting, but quarter is not supported by SimpleDateFormat.
Does anybody have an alternative for this?
Usually you create a variable in which you return a string that is formatted the way that you want. You may have to use the very simple function such as:
$F{aDate}.getYear() or $F{aDate}.getMonth() etc and use the conditional expression to do if then else like logic. Should be straight forward.
Do you need to have several functions to format a date into several formats for the same field?
This is what I came up with:```
( $P{P_DT_FORMAT} == “yyyy-QQQ” ? (($F{col_data}.getYear()+ 1900) + “-” + ( $F{col_data}.getMonth() < 3 ? “Q1” : ( $F{col_data}.getMonth() < 6 ? “Q2” : ( $F{col_data}.getMonth() < 9 ? “Q3” : “Q4” ) ) ) ) : new SimpleDateFormat($P{P_DT_FORMAT}).format($F{col_data}))
This works correctly in iReport when I supply the different date formats in the parameter (yyyy, yyyy-MM, yyyy-QQQ) but when I run this from Servoy I get
> Error evaluating expression :
> Source text : ( $P{P_DT_FORMAT} == "yyyy-QQQ" ? (($F{col_data}.getYear()+ 1900) + "-" + ( $F{col_data}.getMonth() < 3 ? "Q1" : ( $F{col_data}.getMonth() < 6 ? "Q2" : ( $F{col_data}.getMonth() < 9 ? "Q3" : "Q4" ) ) ) ) : new SimpleDateFormat($P{P_DT_FORMAT}).format($F{col_data}))
It seems everywhere I go I get beat down by either JasperReports or Servoy.
I get just what I expect to happen: column headers with 2012-Q1, etc.
Only when I use the combined expression (previous post) and try to get the quarter format it gives the error, the other formats work correctly.
ensure you have made the language Java and not Groovy.
the function that you have does not work in iReport so I do not see how it works in Servoy either.
The string “==” operator does not work the way you think it does, use either:
($P{P_DT_FORMAT}.contentEquals( "yyyy-QQQ")
or
($P{P_DT_FORMAT}.equalsIgnoreCase( "yyyy-QQQ")
When I tried this it works fine in iReport.
It should also work from Servoy plugin now.
Just ensure that that the parameter P_DT_FORMAT is always checked for validity before calling the report. A null value should be checked if it is not already.