PrimeFaces calendar change pattern after date selection - jsf-2

I use PrimeFaces calendar component on page. Pattern attribute is set to MM/dd/yyyy HH:mm:ss. Component value attribute is linked to java.util.Date variable in bean. When i open the page date in format which i expect, but after change of the value, format will always change into different format.
Expected format - 06/15/2015 08:00:30
Format after change of value - 06/15/2015 8:00 AM
Component ussage.
<p:calendar value="#{someBean.someJava.UtilDateVariable}" pattern="MM/dd/yyyy HH:mm:ss" locale="en">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss"/>
</p:calendar>
I already tried to check primefaces showcase and documentation, different locale specification and also usage of the localization script for current localization but with no effect.
Every suggestion is welcome.

You do not need <f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss"/>. This tag converts String to Object and viceversa.
The Primefaces documentation only uses the Pattern attribute and sometimes the locale attribute causes troubles.
Try:
<p:calendar value="#{someBean.someJava.UtilDateVariable}" pattern="MM/dd/yyyy HH:mm:ss"/>

I know this is old, but I had simalar problem, calendar showing right date, but when I opened the datepicker the pattern seemed to be wrong.
After three hours I ended it was bootstrap pattern.
To solve quickly I included a javascript file on the jsf page with setting for pattern:
In XHTML
<h:outputScript library="js" name="myscripts.js" />
IN JS
$(document).ready(function() {
$.fn.datepicker.defaults.format = "dd/mm/yyyy";
});

Related

Primefaces Calendar Setting Date Value in Backing Bean

I am using Primefaces inline (no popup) calendar on my JSF page. When the page is initially loaded, the calendar shows today's date. Initially the backing bean's value attached to calendar's date is empty. Now when I submit the form without selecting a date (no mouse select), the today's date value is not being set in backing bean. Isn't this a bug? Why am I forced to select a date when the date is already highlighted on a calendar when page is loaded?
Primefaces 3.4.2, JSF 2.2, Glassfish 4
<h:form id="calendarFormId">
<p:calendar id="calendarId" mode="inline" value="#{eventController.eventUI.date}" widgetVar="calendarWidget">
</p:calendar>
</h:form>

Struts2 : internationalize s:date?

<s:date name="mydate" format="dd/MM/yyyy HH:mm" /> works perfectly for fomatting my date to the french standard.
But It's hard-written in my jsp. So I have to build if clause to switch according to the locale.
Is there a way to put this format to a general property file?
I tried:
format.date = {0,date,dd/MM/yyyy}
format.time = {0,time,HH:mm}
put in my global .properties but it's not taken into account when, I try just a <s:date name="mydate"> or a <s:property value="mydate"/>.
Create localized date format inside your properties files, e.g. with struts.date.format key:
struts.date.format = dd.MM.yyyy
And use getText method to get this date format in <s:date> tag format attribute:
<s:date name="date" format="%{getText('struts.datetime.format')}"/>
Absolutely. Internationalization (i18n) is handled out-of-the-box in almost every framework out there, then it's better to spend time on understand how to make it works than to write some unneded, buggy custom solution.
With Struts2, you must ensure to have:
I18nInterceptor in your Interceptor Stack;
I18nInterceptor defined in struts.xml: <constant name="struts.custom.i18n.resources" value="global" />;
the localized, correctly named global.properties file for each locale you want to handle;
the request_locale` parameter (if you have not changed the language in your browser) when calling your Actions, giving the I18nInterceptor the ability to hijack the request to the correct resource;
an appropriate character encoding on your pages: <%# page contentType=”text/html;charset=UTF-8″ %>.
Take a look at the guides available on the web too:
http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
http://www.2bloggers.com/2011/12/localization-i18n-in-struts2.html
http://www.roseindia.net/struts/struts/struts2.2.1/tags/i18ntag.html
http://www.roseindia.net/struts/strutsinternationalization.shtml
http://www.roseindia.net/tutorials/I18N/internationalison-code.shtml

Input Date in Struts2

How to change the format of java.util.Date object in struts 2. What I have tested, Struts 2 accepts m/d/yy. Is it possible to change the format?
And also how to change the default message "Invalid field value for field date"?
date tag in struts2 can only be used in output.
You can pass format to s:date tag
<s:date name="myDate" format="yyyy-MM-dd" />
To change the default error message, in your validation.xml you can add message parameter
<field name="myDate">
<field-validator type="date">
<message key="errors.myDateRequired" />
</field-validator>
</field>
Then in your ApplicationResources.properties file you will provide value for errors.myDateRequired
Update: As per comment
s:date is only a read only tag which means cannot be used to accept input from user.
For enabling textField to accept date in different format, couple of approaches:
Use struts2-jQuery plugin, it has a very nice date picker which support different date timeformat, showcase, DatePickerTag.
Custom javascript code to format user input.
Use jQuery (if allowed) and use jQuery datepicker
You can use custom converter to use text field for date, example
The date format Struts accepts depends on the user's locale. So for different locales you can have different date formats. This way you won't be able to support multiple date formats for the same locale but I don't see why you would need this anyway, it's only going to confuse your users. The only way to support different date formats for the same locale without javascript would be with a custom date converter as #mprabhat notes.
You could see the official doc about this:
Define the format for every language y the resource bundle. For instance, an appropriate en_US format definition extension could look like this:
format.date = {0,date,MM/dd/yy}
In parallel, you could add the following to your de_DE bundle:
format.date = {0,date,dd.MM.yyyy}
http://struts.apache.org/2.1.6/docs/formatting-dates-and-numbers.html

Display Current Date on JSF Page

Is it possible to display the current date (today's) in JSF without using a backing bean?
I have the following code snippet , but it didn't work out.
<div class="leftSide">Today's date #{currentDate}</div>
or
<f:facet name="header">
<h:outputText value="Today's date" />
</f:facet>
<h:outputText value="#currentDate">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
You could register an instance of java.util.Date as a request scoped bean in faces-config.xml.
<managed-bean>
<managed-bean-name>currentDate</managed-bean-name>
<managed-bean-class>java.util.Date</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
This way it's available as #{currentDate} without the need for a custom backing bean class.
Update: the JSF utility library OmniFaces has such a bean already registered as #{now}. So if you happen to use OmniFaces already, you can just make use of it directly.
<h:outputText value="#{now}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
In JSF you could use the implicit EL object session that provides access to the current HttpSession. The HttpSession#getLastAccessedTime time ...
... returns the last time the client sent a request associated with this
session, as the number of milliseconds since midnight January 1, 1970
GMT, and marked by the time the container received the request.
So you could use the following code snippet in your facelet:
<h:outputText value="#{session.lastAccessedTime}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
This will be server time and may differ from client time with respect to different time zones.
But you could also use a javascript solution, as discussed here:
How do I get the current date in JavaScript?
Or you could do it using Omnifaces. I'm surprised BalusC hasn't told you about this solution (I think he's a great Omnifaces contributor). Maybe it's because using Omnifaces just to display a simple date in a page might be an overkill to some.
Anyway, if your project already uses Omnifaces, there are 2 managed beans exposed by default and one in particular that you may find handy. As per the tag documentation specifies, once Omnifaces is added to your project, you can use the #{now} managed bean.
For instance, to set a Primefaces calendar's max date, I just wrote the following :
<p:calendar id="myCalendar" pattern="dd/MM/yyyy"
value="#{mybean.myDate}" maxdate="#{now}"/>
I guess the #{now} managed bean can be used in many more situations, and probably yours as well.
If your project does not use Omnifaces yet, I suggest you look at their spec and see how helpful it could be for you.
For instance, I'm using their validateOrder tag to make sure two dates are properly ordered.
You can use the tag which PrimeFaces provide.
<p:clock pattern="HH:mm:ss dd-MM-yyyy"/>

jqueryui datepicker dd/MM/yy culture related issue

I'm using a jQueryUI datepicker for a simple "From" & "To" date range filter in my ASP.Net MVC web-app. Works fine until I hit a machine which had its datetime format (in the Regional Settings) set to "dd-MM-yy".
In my controller I've a postback action which accepts a custom search object as a parameter which is then passed to the search function. This custom object has the properties for "To" & "From" dates and as usual the binding takes place automatically. Works normal on a machine with date format "MM/dd/yy" but if I set it to "dd/MM/yy" in the regional settings then it is unable to parse the date.
The easiest solution could be to change the datepicker's format to
"dd-MMM-yyyy" a non-culture specific date format but my client wants
the selected date to be displayed in MM/dd/yyyy format.
Any clean and easy suggestion to handle this at the datepicker or controller level?
You can use an hidden field with a custom format:
HTML:
<input name="from" type="text" id="from" />
<input name="hiddenFrom" type="hidden" id="hiddenFrom" />
Javascript:
$("#from").datepicker({
altField: "#hiddenFrom",
altFormat: "dd-mm-yy" // or whatever
});
You'll find documentation about jquery ui date format here

Resources