0

大家好,我遇到了自定义属性编辑器的问题。以下是我的代码*片段: *

  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();
         }}
   }

从上述getAstext方法中,我将Calendar对象转换为日期,以便以弹簧形式显示,如下所示:

form:input path="dateTime" value=" ${catalogingInfo.dateTime}"

getAstext正在被调用,但日期未以正确的格式显示,它显示如下:

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=5,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=6,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]

我不确定我在哪里犯了错误。对不起,我的英语不好。

4

1 回答 1

1

对,因为您不能使用 toString 来格式化日期或日历。使用 SimpleDateFormat 类和格式化操作

于 2012-07-31T14:24:35.340 回答