You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by "Leonardo Uribe (JIRA)" <de...@myfaces.apache.org> on 2008/07/29 16:08:32 UTC

[jira] Commented: (MYFACES-1893) DateTimeConverter date format for converter exception args

    [ https://issues.apache.org/jira/browse/MYFACES-1893?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12617794#action_12617794 ] 

Leonardo Uribe commented on MYFACES-1893:
-----------------------------------------

Checking the full code on DateTimeConvert.java:

    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
    {
        if (facesContext == null) throw new NullPointerException("facesContext");
        if (uiComponent == null) throw new NullPointerException("uiComponent");

        if (value != null)
        {
            value = value.trim();
            if (value.length() > 0)
            {
                DateFormat format = getDateFormat();
                TimeZone tz = getTimeZone();
                if( tz != null )
                    format.setTimeZone( tz );
                try
                {
                    return format.parse(value);
                }
                catch (ParseException e)
                {
                    try {
                        String type = getType();
                        Object[] args = new Object[]{value,format.parse(new Date().toString()),_MessageUtils.getLabel(facesContext, uiComponent)};
                        
                        if(type.equals(TYPE_DATE))
                            throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATE_ID,args));
                        else if (type.equals(TYPE_TIME))
                            throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,TIME_ID,args));
                        else if (type.equals(TYPE_BOTH))
                            throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATETIME_ID,args));
                        else
                            throw new ConverterException("invalid type '" + _type + "'");
                    }catch(ParseException exception) {
                        throw new ConverterException(exception);
                    }
                }
            }
        }
        return null;
    }

The code that causes the exception should be changed as suggested. The relevant code just create an array of Object to be used as substitution to create a message. There is no reason to convert to a String and then try to parse it and convert it to a Date. 

The correct solution is do this:

    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
    {
        if (facesContext == null) throw new NullPointerException("facesContext");
        if (uiComponent == null) throw new NullPointerException("uiComponent");

        if (value != null)
        {
            value = value.trim();
            if (value.length() > 0)
            {
                DateFormat format = getDateFormat();
                TimeZone tz = getTimeZone();
                if( tz != null )
                    format.setTimeZone( tz );
                try
                {
                    return format.parse(value);
                }
                catch (ParseException e)
                {
                    String type = getType();
                    Object[] args = new Object[]{value,format.format(new Date()),_MessageUtils.getLabel(facesContext, uiComponent)};
                    
                    if(type.equals(TYPE_DATE))
                        throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATE_ID,args));
                    else if (type.equals(TYPE_TIME))
                        throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,TIME_ID,args));
                    else if (type.equals(TYPE_BOTH))
                        throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATETIME_ID,args));
                    else
                        throw new ConverterException("invalid type '" + _type + "'");
                }
            }
        }
        return null;
    }

So in the message the sample date format is the expected date format to be parsed on the outer try.


> DateTimeConverter date format for converter exception args
> ----------------------------------------------------------
>
>                 Key: MYFACES-1893
>                 URL: https://issues.apache.org/jira/browse/MYFACES-1893
>             Project: MyFaces Core
>          Issue Type: Bug
>          Components: General
>    Affects Versions: 1.2.2, 1.2.3, 1.2.4-SNAPSHOT
>         Environment: Windows XP, Pentium 4
>            Reporter: Matthew Purland
>         Attachments: DateTimeConverter.java.patch
>
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> DateTimeConverter in getAsObject for
>                 		Object[] args = new Object[]{value,format.parse(new Date().toString()),_MessageUtils.getLabel(facesContext, uiComponent)};
> throws ParseException Unparseable date: "Thu Jul 10 11:05:21 CDT 2008" 
> The 
> This is because format.parse(new Date().toString()) is an incorrect format for the DateFormat instance.  This is because there is no default SimpleDateFormat instance to format the pattern "Thu Jul 10 11:05:21 CDT 2008"
> This should be changed to use the format method for formatting Date objects as documented in both Date and DateFormat javadoc.
> Please change to:
> format.format(new Date())

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.