0

I am almost finished with a page that builds out it fields dynamically and sets validation of data types using regular expressions, but ran into an issue on Friday when I started tying the fields to actual data types instead of just Strings. The problem is with values tied to Dates (java.util.Date) !!

While testing out the page and validations I am populating the date field with a date such as 12/12/2012. When it reaches the validate method it is being shown as Tue Dec 11 19:00:00 EST 2012. The date being shown is always a day earlier @ 19:00:00 and in a completely different format.

What can I do to get the date sent in the format I specified ?

...also...

Any ideas on why the date value is not correct as well ?

Building the date field on the page:

<h:inputText id="inputField2" tabindex="#{field.fieldDefinition.fieldOrder}" value="#{field.val}"  validator="#{field.fieldDefinition.validate}" validatorMessage="#{field.fieldDefinition.validationMessage}">
    <f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>

The Validation method being called:

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
    if (getDataType() != null && getDataType().getValidationExpression() != "") {
        regexValidator = new RegexValidator(); 
        regexValidator.setPattern(getDataType().getValidationExpression()); 
        regexValidator.validate(context, component, value); 
    } 
} 

As always...thanks for the help.

Regards,

Mike

MikeR
  • 633
  • 8
  • 21

2 Answers2

1

The regex validator accepts a String, not a Date. Get rid of that validator, you don't need it for Date at all. If the input format was invalid, you'd already have gotten a conversion error from <f:convertDateTime> and the validator would never be hit at all.

As to why the format is different, that's just the default format of Date#toString(), see also the javadoc. It's exactly that format when you do a System.out.println(date). But you don't need to worry about this if you get rid of the validator.

As to why it's a day earlier, that's just because JSF uses by default GMT timezone, but the Date#toString() uses by default system platform default timezone, see also the javadoc. But you don't need to worry about this if you get rid of the validator.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC. After removing the validators for the Date it still did have the wrong date showing in the DB as to what was entered on the screen. I ended up having to adding javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE to the web.xml which set everything right..atleast for now. Thanks again for the help!!! – MikeR Dec 10 '12 at 16:26
  • You're welcome. As to the timezone problem, that would indeed still happen if you also store the time part of the datetime in the DB even though you're only taking the date part of the datetime. – BalusC Dec 10 '12 at 16:27
0

You should set the timezone in the <f:convertDateTime />, have a look at this post for more details: f:convertDateTime displays wrong Date

Community
  • 1
  • 1
Robin
  • 538
  • 1
  • 3
  • 13
  • I added the locale="en_US" to the h:convertDateTime, but it had no affect on the time being changed. – MikeR Dec 10 '12 at 15:10