1

In any case I get exception Could not parse date.

There is unified company standard of date format - 'dd/MM/yyyy'

There are computers with different system locales. I am using jQueryUI for datepicker ( it is standard for widgets and already settled css styles to match application theme).

At the beginning, I didn't found better solution then manually convert date string to date object using SimpleDateFormat object. Now I have converter class but still need to configure every Action that has java.util.Date property.

I am using XML configuration and I did try to add into struts.xml:

<constant name="struts.date.format" value="dd/MM/yyyy" />

It didn't work.

Is there anything that can force all web-application to use single date format without locales just a unified format for output and input date?

Or the way how I can extract date format expected by Struts date type converter, so I can use JS to convert date before to be sent to server to match Struts expectations?

Roman C
  • 49,761
  • 33
  • 66
  • 176
simar
  • 1,782
  • 3
  • 16
  • 33
  • jQuery uses different date-time pattern then Struts2. – Aleksandr M Apr 15 '14 at 08:44
  • In jqueryui possible to assign date pattern. Point is to sync what produce jqueryui datetimepicker and what expect to consume DateConverter of struts param interceptor. – simar Apr 15 '14 at 09:17
  • If you found this question because you're facing the problem of Struts using inconsistent date parsing based on the browser's `Accept-Language` header, see https://stackoverflow.com/a/68218656/687315 for a custom interceptor to hard-code the locale. – quietmint Jul 02 '21 at 01:43

1 Answers1

0

Struts2 is designed to behave differently according to a locale requested by the client.

i18n interceptor is a member of the default interceptor stack. So, it is responsible for setting requested locale to the action context, overriding the default settings. It also stores that locale to the session for further retrieving it if the locale is not requested by the request parameter.

The action context's locale is used by the default type converter, so setting a locale to the action context should probably override the default locale setting which could be configured via struts.locale and i18n interceptor doesn't change this.

If so, no other actions required, just put your date format to the global resource properties according to that locale. You can also use action properties that could be used with i18n requested locale that has different format due to the order of processing localization resources.

If you don't need that possibility of switching a locale by the user you should remove i18n interceptor from the stack.

How to format the dates and numbers using action context's locale: Formatting Dates and Numbers.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I did try to add date format into to global resources that didnt work out. According to logging, date properties are handled by com.opensymphony.xwork2.conversion.impl.DateConverter class which trying to parse date using different formatters, but non of them is build based on data format from global resources. – simar Apr 16 '14 at 05:52
  • You need not only to add it but use it. First thing is this converter is doing is getting a locale from the context, then creates a formatter/parser based on that locale, then do the job. All you need is to format your dates to correct locale. – Roman C Apr 16 '14 at 09:08
  • That i did already. I did not find the way to configure date format. So i just debug DateConverter to find version of date format which is acceptable by DateConverter and convert by js before to be sent to server – simar Apr 16 '14 at 11:16
  • No, you didn't. I have placed a link that you should follow to format dates and numbers with examples on the page. – Roman C Apr 16 '14 at 11:26
  • 1
    i spent a lot of time with doc trying struts to use proper locale. didn't work. Very often example code from struts doc works only in examples. – simar Apr 16 '14 at 13:37
  • @simar Don't lie yourself if you had spent more time you'd get it working. Very often people refer to the problem that they are lazy to resolve. – Roman C Oct 17 '15 at 08:24