Announcement Announcement Module
Collapse
No announcement yet.
Property Editors getAsText() method not getting used for displaying Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Property Editors getAsText() method not getting used for displaying

    Hello all i am stuck in problem with custom property editors.Following are my code *fragments:*
    Code:
         import java.beans.PropertyEditorSupport;
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Calendar;
      import java.util.Date;
      import org.joda.time.format.DateTimeFormatter;
      import org.joda.time.format.ISODateTimeFormat;
      public class CustomCalendarEditor extends PropertyEditorSupport {
    
             private  Date date;
             @Override
             public String getAsText() {
               Calendar value = (Calendar) getValue();
               Date dateTime = value.getTime();
               System.out.println("in editor--->"+dateTime);
               return dateTime.toString();
             }
             @Override
             public void setAsText(String text) throws IllegalArgumentException {
               try { 
                   DateFormat formatter ; 
                   Date date ; 
                   formatter = new SimpleDateFormat("dd/MM/yyyy");
                   date = (Date)formatter.parse(text); 
                   Calendar cal=Calendar.getInstance();
                   cal.setTime(date);
                   System.out.println("Today is " +date );
                   setValue(cal);
              }catch(Exception e){
                   e.printStackTrace();
             }}
          }
    From above getAstext method I am converting Calendar object to date for displaying in spring form like following:
    Code:
    form:input path="dateTime" value=" ${catalogingInfo.dateTime}"
    getAstext is getting called but date is not displayed in proper format it is showing like
    Code:
     java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=6,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=11,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]
    I am not sure where I am making a mistake. Sorry for my bad English.
    Last edited by piyushpatel2809; Aug 1st, 2012, 05:31 AM.

  • #2
    There is nothing in your getAsText method that is formatting your date there is only a toString nothing for formatting.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      There is nothing in your getAsText method that is formatting your date there is only a toString nothing for formatting.
      Yeah i know..... but it should display the toString() of java.util.Date on my form not of GregorianCalender.

      Comment


      • #4
        To enable property editors you need to configure a WebDataBinder and attach the editor. For this you need a method annotated with @InitBinder if you don't do this the editor will not be used. Also if you are on Spring 3.1 I suggest you take a look the the @DateFormat annotation and formatters in general (instead of property editors).

        Comment

        Working...
        X