Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DateItem / DateTimeItem formats

    Hello,

    I'm using SmartGWT 2.1 and am having issues with date / datetime formats.

    I need different custom (European dd.mm.yyyy style) display and input formats for DateItems and DateTimeItems (one with the time for the latter, obviously). I have set the date formats globally:

    Code:
    DateUtil.setShortDateDisplayFormatter(new MyDateFormat());
    DateUtil.setNormalDateDisplayFormatter(new MyDateFormat());
    but this sets the date formats for both DateItem and DateTimeItem. Is there a way of setting different custom formats for the two types of date inputs? Could I for example set different short and normal formats, and somehow make DateItem use the short one and DateTimeItem use the normal one? Or should I be setting the formats for every DateItem and DateTimeItem manually?

    If so, how do I set a custom date formatter? The setDateFormatter method seems to accept only a few predefined DateDisplayFormats.

    TY,

    -PM

    #2
    I came up with an evil solution. Please don't make me actually use it :).

    -PM

    Code:
    public class MyDateFormat implements DateDisplayFormatter, DateInputFormatter {
    
        DateTimeFormat dtf = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm");
        DateTimeFormat df = DateTimeFormat.getFormat("dd.MM.yyyy");
    
        public String format(Date date) {
            if(date == null) {
                return null;
            }
            String ret = dtf.format(date);
            if(ret.endsWith(" 00:00")){
                ret = ret.substring(0, ret.indexOf(" 00:00"));
            }
            return ret;
        }
    
        public Date parse(String string) {
            if(string == null){
                return null;
            }
            Date ret;
            try{
                ret = dtf.parse(string);
            } catch (Exception ex){
                ret = df.parse(string);
            }
            return ret;
        }
    }

    Comment

    Working...
    X