Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure locale based date format support in spring

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:

  1. User select language to 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.
  2. User select language to 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

like image 802
Jaiwo99 Avatar asked Oct 07 '13 09:10

Jaiwo99


People also ask

What is locale date format?

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.

What is locale date in Java?

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.

How do you apply localization in spring application?

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.


1 Answers

Since nobody answers my question, I just post one of my solution to solve this problem, it could help others:

  1. I had a request scoped bean, which resolves locale using: 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.
  2. 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.

like image 110
Jaiwo99 Avatar answered Sep 28 '22 14:09

Jaiwo99