我在页面中有日历,但bean中的属性是String,我收到错误

时间:2013-07-24 09:35:11

标签: jsf date calendar richfaces

我正在使用富日历,在网页中我应该有日历,但bean属性是一个字符串我无法将其更改为日期,但在保存表单中我总是有

java.text.ParseException: Unparseable date: "Tue Dec 09 00:00:00 GMT+04:00 2059"
java.text.DateFormat.parse(DateFormat.java:357)
javax.faces.convert.DateTimeConverter.getAsObject(DateTimeConverter.java:398)
org.richfaces.component.UICalendar.getAsDate(UICalendar.java:399)
org.richfaces.renderkit.CalendarRendererBase.getSelectedDate(CalendarRendererBase.java:658)
org.richfaces.renderkit.html.CalendarRenderer.doEncodeEnd(CalendarRenderer.java:451)
org.richfaces.renderkit.html.CalendarRenderer.doEncodeEnd(CalendarRenderer.java:516)
org.ajax4jsf.renderkit.RendererBase.encodeEnd(RendererBase.java:134)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:861)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:242)

我尝试了很多,但没有任何转换器帮助......

任何想法?

这是转换器

@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        String pattern = "yyyy-MM-dd, HH:mm:ss aaa";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date nDate = null;
        if (value == null || value.trim().length() == 0) {
            return nDate;
        }
        try {
            nDate = sdf.parse(value);
        } catch (ParseException ex) {
        }
        return nDate;

    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uIComponent, Object value) {
        String date = "";
        try{
        if (value == null) {
            return "";
        }
        String pattern = "yyyy-MM-dd, HH:mm:ss aaa";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date dateValue = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss aaa", Locale.ENGLISH).parse(value.toString());        
        date = sdf.format((Date) dateValue);
        }
        catch(Exception e){          
            e.printStackTrace();
        }
        return date;
    }

 but it does not work

2 个答案:

答案 0 :(得分:0)

尝试这种格式“EEE MMM dd HH:mm:ss z yyyy”进行解析

答案 1 :(得分:0)

使用此命令将String转换为Date,

    String date = "15-09-2011:23:30:45";//your date
    DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy:HH:mm:SS");
    Date convertedDate = (Date) formatter.parse(date);