Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Richfaces Calender

 
Avik Mazunder
Ranch Hand
Posts: 39
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the richfaces calender and set the pattern datePattern="dd MMM, yyyy". In the client side it is showing as I desired. Suppose 13 oct, 1976. But when ever I am saving it in database its saving like this Wed Oct 13 00:00:00 IST 1976. What should I do to save it in my desired form.

 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Before storing date into database you can format it. Example :

It is really good to use converter here to convert from String into formatted Date by method getAsObject of javax.faces.convert.Converter
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The datePattern in the rich:calendar control converts from a Date object to String representation and String to Date. You're saving the raw date in your database, and when you see it as Wed Oct 13 00:00:00 IST 1976, that's because of the formatting pattern of whatever tool you're using to view the database. You can probably update that to whatever you prefer, but how you do it depends on the tool you're using. For example, in Oracle SQL Developer, you'd go into Tools -> Preferences, select Database, NLS Parameters, and change the Date Format field.
 
Avik Mazunder
Ranch Hand
Posts: 39
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Hello!

Before storing date into database you can format it. Example :

It is really good to use converter here to convert from String into formatted Date by method getAsObject of javax.faces.convert.Converter


By this can i get the date of richfaces calender??If yes, then how am i suppose to get that??
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

If I understand correctly question the answer is the selected value in richfaces calendar is stored in bound bean's property.
In your demo you store selected date into property staffDOB of RegistrationBean by :
value="#{RegistrationBean.staffDOB}"
Of course staffDOB must be of type Date.
But I recommend to use converter to format date into needed format before it is stored in staffDOB property. This way staffDOB property will be assigned to Date you format in converter. You can use my code of SimpleDateFormat to format date in converter.
Look at example of simple converter at http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. The calendar component converts the string date into a Date object, which is set to a property in your managed bean. It could be converted back to a string before being stored in the database, but of course this would mean altering your database schema to accept a string in that field. it also would make date operations like sorting or selecting by a date range much harder. I don't recommend doing this.

As I said already, the date is not being stored incorrectly in your database. Your database tool is just using a different format string when it displays dates back to you. You can change that if you want. It has no affect on your application at all.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, databases can be quite treacherous when it comes to date/time data. The granularity of the various Java objects and the granularity of the database objects often have no direct correspondence. For example, in Oracle, a date may have a granularity in seconds, but the closest Java object that contain such values has millisecond granulatity. If you manipulate those objects and then write them to the database, some precision will be silently lost. It doesn't help that there's no real standard for the precisions of the various date/time objects between the different database vendors.

I've found that the best way to consider dates in such cases is to treat them the same way that you would treat floating-point numbers. Among other things, that means don't use them as database keys for "compare-equal" operations.

Also, when storing date and time data into a database from a webapp, I recommend normalizing the value (if needed) to UTC timezone unless you are totally certain that all users will operate from a single timezone.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic