Announcement

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

    question about customizing date format for DateItem

    i use DateItem, i want to display the date in text field as "MM/DD/YY" format, but it is not included in DateDisplayFormat enum, how can i customize the date display format for DateItem??

    #2
    it seems the DateUtil can help,
    original code:
    Code:
            firstDateChoice = new DateItem();
            firstDateChoice.setUseTextField(true);
            firstDateChoice.setPickerIconSrc(...........);
            firstDateChoice.setPickerIconWidth(26);
            firstDateChoice.setPickerIconHeight(26);
    it can not display the date as format of "MM/DD/YY". So i change the code to:
    Code:
            DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
    
                public String format(Date date) {
                    if (date == null) {
                        return null;
                    }
                    //you'll probably want to create the DateTimeFormat outside this method.
                    //here for illustration purposes
                    DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MM/DD/YY");
                    String format = dateFormatter.format(date);
                    return format;
                }
            });
    
    
            firstDateChoice = new DateItem();
            firstDateChoice.setUseTextField(true);
            firstDateChoice.setPickerIconSrc(...........);
            firstDateChoice.setPickerIconWidth(26);
            firstDateChoice.setPickerIconHeight(26);
    unfortunately, wheather i choose any date, the text field ALWAYS display "08//", while i change to "DateUtil.setNormalDateDisplayFormatter" (regarding to the api document, i can not identify the differences of these two method.), it also does not follow the format i required.
    who can help?

    Comment


      #3
      hi zbc,
      guess i found kind of a solution:
      Here is the code:

      Code:
      DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
      	@Override
      	public String format(Date date) {
      		if (date == null) {
      			return null;
      		}
      		final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy | HH:mm");
      		String format = dateFormatter.format(date);
      		return format;
      	}	
      });
      
      DateItem dateItem = new DateItem();
      dateItem.setUseTextField(true);
      dateItem.setAttribute("dateFormatter", "toNormalDate");
      I guess the important point is to set "dateFormatter" to "toNormalDate" as mentioned in the documentation.

      Comment

      Working...
      X