It's not a secret anymore!
The moose likes JSF and the fly likes f:convertDateTime and data type conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "f:convertDateTime and data type conversion" Watch "f:convertDateTime and data type conversion" New topic

f:convertDateTime and data type conversion

Anthony Sykes
Ranch Hand

Joined: Aug 25, 2007
Posts: 57
I have a facelet page with one field for the date and another for the time. I am trying to use the f:convertDateTime to format the fields. This is where things begin to get confusing. It seems as if the f:convertDateTime only works with the Date object. Isn't the Date object marked for deprecation? I also seem to be having a problem with the time information field, which is a Date object. For some strange reason, the time information seems to get lost and I end up with a default time of 00:00:00. Any help would be greatly appreciated.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

No, Date isn't deprecated, although the "brute-force" initializers for month, day and year have been since about Java 1.2 (in favor of Calendars).

Date is like floating-point in that it can really zap you. Officially, it has a granularity of milliseconds. But a number of things can affect that. One of the prime offenders is databases. Date objects are often stored to SQL date database objects that have a different granularity, resulting in loss of data. Some databases define a date object as a true date with a granularity in days. Some are precise to seconds, and I think one even has 1/10th-second granularity. It can be a major headache, especially if you're unwise enough to attempt to use a date or timestamp as a database index item. Then there's alternative "Date" classes like TimeStamp, as well as alternative database SQL types...

On top of that, the JSF convertDateTime has its own idiosyncrasies, so it's not a bad idea to keep the antacids nearby.

An IDE is no substitute for an Intelligent Developer.
Augustine Chelliah

Joined: Mar 12, 2009
Posts: 28
The convertDateTime is an excellent tag which I use widely in my project. Probably you might have encountered some problem in setting the date object.

1. If you are getting the date from the database whic is of type 'timestamp' or 'Date', then we need to convert the obtained date to java.util.Date.
If timestamp,
TimeStamp time = <timestamp from database>
new Date(time.getTime());
This snippet will work while using convertDateTime in the frontend.
If 'Date',
Just a typecast would suffice

2. If you are mannually setting the date in the java code, make sure you set the time information also; else the time information will show as 00:00:00 in the page. I dont think there are any way in which the time information gets lost.

3. If you are getting date in the form of string[say from Mainframe, or an inputText], then you can use simpleDateFormatter to convert.

Hope I have answered your question

Thanks & Regards,
I agree. Here's the link:
subject: f:convertDateTime and data type conversion
It's not a secret anymore!