You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jim Pinkham <pi...@gmail.com> on 2011/01/24 16:37:23 UTC
How to make DateTimeField default to PM
I wanted to have my DateTimeField (That's the wicket-extensions component
with a date text field, button to open calendar, and hours/minutes fields
with optional AM_PM choice) default to PM instead of AM for new dates
because I noticed most dates in this part of my app are evening dates.
I came up with this; maybe it helps someone, or let me know if you have a
better way:
FormComponent<Date> dateField = new DateTimeField("eventOn") {
private static final long serialVersionUID = 1L;
// amOrPmChoice is final, so we have a little kludge to
default new item's eventOn date to PM
// TODO: find a less clever way
@Override
protected void onBeforeRender() {
Date d = (Date)getDefaultModelObject();
if (d == null) { // user hasn't entered a date (yet)
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 18); // anything after
noon would work
IModel<Date> dm = (IModel<Date>) getDefaultModel();
dm.setObject(c.getTime());
super.onBeforeRender(); // initializes
amOrPmChoice as side-effect
dm.setObject(null);
}
super.onBeforeRender();
}
@Override
protected DateTextField newDateTextField(final String id,
final PropertyModel<Date> dateFieldModel) {
return DateTextField.forDatePattern(id, dateFieldModel,
"MM/dd/yyyy");
}
};
Cheers,
-- Jim.