Sunday, February 1, 2009

Doing date formatting in complex expression

From time to time you might want to build a whole string with many variables/fields without having to "butt" two different fields next to each other.

Create a new Text Field and put in an expression like

$F{First_name} + " " + ${Last_name}

Clean and the text will align much better. Now what is one of the fields is a date?

$F{First_field} + " " + (new SimpleDateFormat("MM/dd/yyyy")).format($F{some_date_field})

Java is too the rescue. Jasper expressions can do Java for the most part and this can come in handy. Read the SimpleDateFormat API docs for the full pattern variables available.

No comments:

Post a Comment