PDA

View Full Version : DateField alternative formats



fgd
13 Jul 2009, 3:39 AM
Is there a way to specify alternative formats for DateField?

I would like DateField to convert from different strings to the defined DateTimePropertyEditor format

ie: "01022009" or "010209" to convert to the same format of "01.02.2009"

Is that possible?

fgd
5 Oct 2009, 1:26 PM
Any takers?

yaittahar
6 Oct 2009, 3:10 AM
DateField dateField =new DateField();
DateTimePropertyEditor dateFormat = new DateTimePropertyEditor("dd/MM/yyyy");
dateField.setPropertyEditor(dateFormat);

for more details see javadoc com.google.gwt.i18n.client.DateTimeFormat

fgd
6 Oct 2009, 3:20 AM
DateField dateField =new DateField();
DateTimePropertyEditor dateFormat = new DateTimePropertyEditor("dd/MM/yyyy");
dateField.setPropertyEditor(dateFormat);

for more details see javadoc com.google.gwt.i18n.client.DateTimeFormat

I understand this. But this is used to specify the format in which a Date should be displayed. There is no way to specify "alternative formats" which are used for input processing.

Example:
If user types in "10122009" the input should be parsed to create a Date and then DateTimePropertyEditor formats it to "10.12.2009" for display.

Another example: "101209" should be parsed into Date and displayed as "10.12.2009".

Basically, I would like to duplicate the same behavior as GWT-Ext's DateField has with this config:


this.setFormat("d.m.Y");
this.setAltFormats("dmY|dmy|j.n.y|j.n.Y");

fgd
8 Oct 2009, 12:55 AM
Since nobody came up with a solution, this is how I've solved the problem:



DateField dateField = new DateField()
DateTimePropertyEditor pe = new DateTimePropertyEditor(DateTimeFormat.getFormat("dd.MM.y"));
dateField.setPropertyEditor(pe);

dateField.addListener(Events.Blur, new Listener<FieldEvent>(){
public void handleEvent(FieldEvent be) {
Log.debug("Events.Blur for " + getFieldLabel());

DateField df = (DateField) be.getField();

if(df.getRawValue() != null && df.getRawValue().length() > 0 && !df.getRawValue().contains(".")) {
try {
String raw = df.getRawValue();
if(raw.length() == 6)
df.setValue(DateTimeFormat.getFormat("ddMMyy").parse(raw));
else if(raw.length() == 8)
df.setValue(DateTimeFormat.getFormat("ddMMyyyy").parse(raw));

df.fireChangeEvent(be.getOldValue(), df.getValue());

} catch (Exception e) {
Log.error("Error: " + e.getMessage(), e);
}
}
}
});

I hope someone finds it useful.

I still think this should be implemented in GXT like it is in ExtJS.