Announcement

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

    Date formats and GWT's LocaleInfo

    Hello,

    Should smartgwt's date related components honor the GWT's standard DateTimeFormatInfo ? I'm running the application in finnish locale and LocaleInfo.getCurrentLocale().getDateTimeFormatInfo() getters return formats as follows:

    [INFO] Running on locale: fi
    [INFO] dateFormat: d.M.yyyy
    [INFO] dateFormatFull: EEEE d. MMMM y
    [INFO] dateFormatLong: d. MMMM y
    [INFO] dateFormatMedium: d.M.yyyy
    [INFO] dateFormatShort: d.M.yyyy

    These values are right but still smartgwt renders the dates in listgrid cells in "mm.DD.yyyy" for some reason.

    I know there are setters in DateUtil to finely control the formats for rendering and inputting but I'm really trying to avoid hard coding all locales when all formats are provided by the gwt platform anyway.

    Using smartgwt lgpl 3.0.

    #2
    The locale files that come with SmartGWT generally change the date formatter. If yours does not have the right date format you can update it here.

    Otherwise if you prefer to use GWT's DateTimeFormatInfo you can apply those formats system-wide with a couple of calls to DateUtil.

    Comment


      #3
      Ok. I tried looking into finnish translation file but I did not understand what keys I should exactly set to get the formats correct. Or atleast most of the (european) localization files did not include the correct date formats.

      For anyone else with the same issue this is the code I'm now using to honor the GWT's LocaleInfo:

      Code:
              DateTimeFormatInfo i = LocaleInfo.getCurrentLocale().getDateTimeFormatInfo();
      
              final String shortDate = i.dateFormatShort();
              final String shortDateTime = shortDate + " " + i.timeFormatShort();
              final String normalDate = shortDate;
      
              final DateTimeFormat shortDateFormat = DateTimeFormat.getFormat(shortDate);
              final DateTimeFormat shortDateTimeFormat = DateTimeFormat.getFormat(shortDateTime);
              final DateTimeFormat normalDateFormat = shortDateFormat;
      
              DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
                  @Override
                  public String format(Date date) {
                      if (date == null)
                          return null;
                      return shortDateFormat.format(date);
                  }
              });
      
              DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter() {
                  @Override
                  public String format(Date date) {
                      if (date == null)
                          return null;
                      return shortDateTimeFormat.format(date);
                  }
              });
      
              DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
                  @Override
                  public String format(Date date) {
                      if(date == null)
                          return null;
                      return normalDateFormat.format(date);
                  }
              });
      
              // try to guess DMY, MDY or YMD from shortDate ...
              StringBuilder dmy = new StringBuilder();
              for(char c : shortDate.toCharArray()) {
                  switch(c) {
                      case 'd':
                          if(dmy.indexOf("D") == -1)
                              dmy.append("D");
                          break;
                      case 'M':
                          if(dmy.indexOf("M") == -1)
                              dmy.append("M");
                          break;
                      case 'y':
                          if(dmy.indexOf("Y") == -1)
                              dmy.append("Y");
                          break;
                  }
                  if(dmy.length() == 3) {
                      DateUtil.setDateInputFormat(dmy.toString());
                      break;
                  }
              }

      Comment

      Working...
      X