Does someone had this problem:
I need to configure spring to recognize locale based date and number format, i.e. the following user behavior should be valid:
EN
, then the number format 1.23
should be valid, spring mvc will accept this format and no valid error triggered. User can also change the date with date format MM/dd/yyyy
and no valid error raised, user can post this form.DE
, then the number format 1,23
should bevalid, spring mvc will accept this format and no valid error triggered. User can also change the date with date format dd.MM.yyyy
and no valid error triggerd. User can post this form.I'v tried to use @DateTimeFormat(pattern="#{messageSource['date_format']}")
,(I have date_format defined in messages_(locale).properties) but seems spring doesn't support this yet, see JIRA ISSUE
Does someone has the similar problem and got a solution.
Does it help to write my own converter, and register it in org.springframework.format.support.FormattingConversionServiceFactoryBean
? I need some kind of request based converter
Java DateFormat The locale is used for specifying the region and language for making the code more locale to the user. The way of writing date is different in different regions of the world.
LocalDate class in Java 8 Furthermore, LocalDate represents the date in ISO format, for example: 2021-01-01 . So let's see how to perform localization with the Java LocalDate class. You can get the current date with the now() function in the LocalDate class: LocalDate currentDate = LocalDate.
The first step towards a localized application is to extract all static text from your templates into a localization file. Later, this is used to look up the required text bits according to the selected language.
Since nobody answers my question, I just post one of my solution to solve this problem, it could help others:
RequestContextUtils.getLocale(request);
request can be autowired to the request scoped class(NOTICE, it works only with field injection, not with construction or setter). In this class I get locale based date/number format.In my controller (we have actually a abstractController). I have code like this:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new LocalizedDateEditor(formatHelper));
}
formatHelper
is the request scoped bean. LocalizedDateEditor
looks like this:
public class LocalizedDateEditor extends CustomDateEditor {
public LocalizedDateEditor(FormatHelper formatHelper) {
super(new SimpleDateFormat(formatHelper.getDefaultDateFormat()), true);
}
}
It just tell spring to use my dateFormat.
That's all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With