I’ve carefully studied Jonik’s entry about customizing BigDecimal formatting in Wicket. Thanks for this excellent piece of code. Unfortunately I can’t get it to work for my use case.
I want to register date formatting globally and am using the following code in the Application subclass:
@Override
protected IConverterLocator newConverterLocator() {
ConverterLocator converterLocator = new ConverterLocator();
converterLocator.set(Date.class, new DateConverter() {
@Override
public DateFormat getDateFormat(Locale ignore) {
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
}
});
return converterLocator;
}
Then when using date fields in the web pages the code is as follows:
form.add(new TextField<Date>("dateField"));
When rendered, the date fields are showing the standard java.text.DateFormat.SHORT (02.11.11 11:59) formatting coming from the org.apache.wicket.util.convert.converter.DateConverter class instead of my custom SimpleDateFormat (02.11.2011 11:59:42).
I’ve checked that java.util.Date is being used throughout. Wicket version is 1.4.12.
Any ideas?
I think your
dateFieldhas typejava.util.Date, but actual object (loaded from database?) is e.g.java.sql.Timestampor some other child ofjava.util.Date. That’s whyConverterLocatorchooses other converter instead of yours. The source code of theConverterLocatorhas the following:So, you need to know the exact run-time type of your
dateFieldand override converter for it.