Jump to content

Date format problem


kunkasurendra

Recommended Posts

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

You should be able to do this in a text field expression. You can make your class of type Date and then your expression can use the date String you are getting and a combination of the DateFormat class and the SimpleDateFormat to convert to the Date you want (this is going to get a bit tricky).

 

Does it have to be of type Date? If it is just for display in the report could you not just format the string using the SimpleDateFormat and display it to the user (so the textfield would be of type String)?

Link to comment
Share on other sites

Surendra,

 

I think the basic goal is this:

Retrieve a string which represents a date from the database and display it in a report in a different format.

 

There are 2 main points to solve:

1. How to convert the string to a date

2. How to format the date well

 

For point one I recommend making the transformation in the SQL. Use functions like Oracle's TO_DATE or MySQL's STR_TO_DATE so that the field returned is a java.util.Date.

 

For point two it will now be easy to format the field using Java:

new SimpleDateFormat("dd.MM.yyyy").format($P{StartDate})

or for better I18N formatting:

DateFormat.getDateInstance(DateFormat.SHORT, $P{REPORT_LOCALE})

 

I find that it's cleanest to understand the problem by breaking it up like that. And it's easier to maintain.

 

On the other hand, lshannon's idea is also possible in your case. In that case you'll need to parse the string into a date in Java:

new SimpleDateFormat("yyyy-MM-dd").parse($F{MyStringField ThatHoldsDateInfo})

 

But then you'll also need to add the code to format this newly created date into the correct display style.

 

Good luck,

Matt

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