Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Formatting for dates/numbers

  1. #11
    Join Date
    Nov 2010
    Posts
    24

    Default

    Ok, let's be more specific (not summarized, I guess).

    I use formatter/validator annotations, and want the @DateTimeFormat to do the job for dates, enforcing the correct format when parsing, and printing the very same format when populating inputs, without the boring task of setting <fmt:formatDate> for each field. The same applies to BigDecimal fields.

    My app have no multilanguage support right now, but it may be added someday, so I'd like to keep it flexible enough to avoid rework in the furure. So I don't want to hardcode date/number patterns everywhere in my code. I want to keep formatting/parsing up to the locale, so that new formats can be supported instantly.
    The question is that my locale uses dd/mm/yy as date format and I need dd/mm/yyyy, and numbers are 0.000,0 , and I need 0,00 (no group separators, two decimal digits if monetary).

    That's what I need, and I don't know how to do it. I don't mind "overriding" the default patterns used by my locale (with custom classes and/or XML config), if that's the way to go... as long as this fix reflects everywhere.

    Thanks, Keith!
    Last edited by mdrg; Dec 8th, 2010 at 09:23 PM.

  2. #12
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    When using @DateTimeFormat("S-"), the "S-" is interpreted as a Joda DateTime "style code" -- we simply delegate to Joda's printing and parsing behavior for that code. You could change what "S-" means, but you'd need to plug in a custom DateTimeFormatAnnotationFormatterFactory to do this. You might consider instead registering a custom Formatter<Date> and Formatter<BigDecimal> that applies to all Dates and BigDecimal fields (so conversion would be driven by type, not annotation, and no field annotation would be required then). To register a custom Formatter, you need to subclass FormattingConversionServiceFactoryBean, override the installFormatters(FormatterRegistry) hook, register your customizaitons, and finally inject the ConversionService produced by this FactoryBean into your mvc:annotation-driven element. You might find it helpful to review the super implementation which installs the default Joda-based @DateTimeFormat support, as well as the default @NumberFormat support.

    I also recommending reading the reference documentation in this area and reviewing the mvc-showcase, which has working examples of customizing the registered set of Converters/Formatters.

    Hope this gets you going.

    Keith
    Last edited by Keith Donald; Dec 8th, 2010 at 09:36 PM.
    Keith Donald
    Core Spring Development Team

  3. #13
    Join Date
    Nov 2010
    Posts
    24

    Default

    Thanks for the tips. In the other topic about the Trimmed annotation I've seen how to implement formatters, but I prefer a per-field definition. I'll try a new DateTimeFormatAnnotationFormatterFactory and NumberFormatAnnotationFormatterFactory tonight and see if it does the trick.

    Just one question: registering my custom formatter factory, like AnnotationFormatterFactory<NumberFormat>, would simply replace the default Spring implementation, or how would it behave? The docs do not mention this.

    Once again, many many thanks, Keith!

  4. #14
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    AnnotationFormatterFactory<A> is a simple interface, so I'd try implementing your own that meets your needs. You would then replace the default implementations with your own. Alternatively, if you believe you can patch the default implementations to allow them to be customized to meet your needs, that'd be cool, too. But you might want to start first with just implementing your own, it's a good exercise to go through.

    Keith
    Keith Donald
    Core Spring Development Team

  5. #15
    Join Date
    Dec 2008
    Location
    Ulaanbaatar, Mongolia
    Posts
    123

    Default

    I can't find DateTimeFormatAnnotationFormatterFactory in spring 3.0.5 code?
    Is that changed to JodaDateTimeFormatAnnotationFormatterFactory?
    Let's care our nature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •