This is neither simple nor clever, but it's one way:

public class WicketDateConverter implements IConverter {
    private SimpleDateFormat format;

    public WicketDateConverter(String formatStr) {
        format = new SimpleDateFormat(formatStr);
    }

    public Object convert(Object value, Class c) {
        if (value instanceof Calendar) value = ((Calendar)
value).getTime();
        return value == null || value.equals("") ? "" :
format.format(value);
    }

    public Locale getLocale() {
        return Locale.getDefault();
    }

    public void setLocale(Locale locale) {
        // won't be paying attention to this anyway
    }
}

Then, in a Page or Panel where you need to display a date,

// if you want to use it in several places on a page
private final WicketDateConverter dateAndTime = new
WicketDateConverter("MMM d, h:mm a");

And for the actual Label:

add(new Label("time") {
        public IConverter getConverter() {
                return dateAndTime;
        }
});

All of this is certifiably nuts, but it's the only way I've found to
crank out dates in whatever format I want for a certain Label. You could
also subclass Label as SimpleDateLabel to avoid having to override
getConverter all the time. Guess I didn't bother that because I figure
the API develop a better way eventually.

Nathan
http://technically.us/n8/

On Tue, 2006-02-14 at 10:27 -0500, Steve Knight wrote:
> Hello,
> 
> I have a Label that displays a java.util.Date, but I'd like to change
> the way it is displayed.  What is the simplest way to change the
> format for the label?
> 
> I tried searching the mailing list but I think it only made me more confused.
> 
> Thanks.
> Steve




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to