这个简单的代码将Date
字符串转换为我想要的格式。但是,在格式化日期时,会添加一个额外的日期。当 onChanged 事件在 SmartGWT 中的 RelativeDateItem 控件上触发时,解析完成。但是,我认为使用该组件不会影响日期解析。
private static String DATE_FORMAT = "dd.MMM.yyyy kk:mm";
...
private void changeDateFormat()
{
DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter()
{
public String format(Date date)
{
System.out.println("setShortDatetimeDisplayFormatter = " + date.toString());
if(date == null)
{
return null;
}
else
{
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat(DATE_FORMAT);
System.out.println("Formatted date = " + dateFormatter.format(date));
return dateFormatter.format(date);
}
}
});
// It is a requirement that we implement a custom date parser or the onChanged event
// will not fire.
DateUtil.setDateParser(new DateParser()
{
public Date parse(String dateString)
{
System.out.println("Entering parse = " + dateString);
final DateTimeFormat format = DateTimeFormat.getFormat(DATE_FORMAT);
System.out.println("Exiting parse = " + format.parse(dateString));
return format.parse(dateString);
}
});
}
调试:
setShortDatetimeDisplayFormatter = Tue Feb 19 00:00:00 EST 2013
Formatted date = 19.Feb.2013 24:00
Entering parse = 19.Feb.2013 24:00
Exiting parse = Wed Feb 20 00:00:00 EST 2013
Entering parse = 19.Feb.2013 24:00
Exiting parse = Wed Feb 20 00:00:00 EST 2013
setShortDatetimeDisplayFormatter = Wed Feb 20 00:00:00 EST 2013
Formatted date = 20.Feb.2013 24:00
它会触发两次,因为RelativeDateItem
控件同时包含选取器文本和相邻的标签文本,因此必须对两者进行格式化。