Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package org.apache.myfaces.commons.converter;
 
 import java.util.Date;
 import java.util.Map;
 
 
Simple convert that overrides the spec DateTimeConverter and uses TimeZone.getDefault() as the base timezone, rather than GMT. Convert date time using normal system timezone like it should User: treeder Date: Oct 28, 2005 Time: 7:19:01 PM
 
    name = "mcc:convertDateTime",
    tagClass = "org.apache.myfaces.commons.converter.ConvertDateTimeTag",
    tagSuperclass = "org.apache.myfaces.commons.converter.ConverterTag",
    tagHandler = "org.apache.myfaces.commons.converter.ConvertDateTimeTagHandler",
    serialuidtag = "1542071733367150635L",
    evaluateELOnExecution=true)
 {
     public static final String CONVERTER_ID = "org.apache.myfaces.custom.convertDateTime.DateTimeConverter";
     
     // internal constants
     private static final String TYPE_DATE = "date";
     private static final String TYPE_TIME = "time";
     private static final String TYPE_BOTH = "both";
     private static final String STYLE_DEFAULT = "default";
     private static final String STYLE_MEDIUM = "medium";
     private static final String STYLE_SHORT = "short";
     private static final String STYLE_LONG = "long";
     private static final String STYLE_FULL = "full";
     private static final TimeZone TIMEZONE_DEFAULT = TimeZone.getTimeZone("GMT");
 
     // CONSTRUCTORS
     public DateTimeConverter()
     {
         //setTimeZone(TimeZone.getDefault());
     }
     
     private String _dateStyle;
     private Locale _locale;
     private String _pattern;
     private String _timeStyle;
     private TimeZone _timeZone;
     private String _type;
     private boolean _transient;
     
     private transient FacesContext _facesContext;
 
     // METHODS
     public Object getAsObject(FacesContext facesContextUIComponent uiComponentString value)
     {
         if (facesContext == nullthrow new NullPointerException("facesContext");
         if (uiComponent == nullthrow new NullPointerException("uiComponent");
 
         if (value != null)
         {
             value = value.trim();
             if (value.length() > 0)
            {
                DateFormat format = getDateFormat();
                TimeZone tz = getTimeZone();
                iftz != null )
                    format.setTimeZonetz );
                try
                {
                    return format.parse(value);
                }
                catch (ParseException e)
                {
                    String type = getType();
                    Object[] args = new Object[]{value,format.format(new Date()),MessageUtils.getLabel(facesContextuiComponent)};
                    
                    if(type.equals())
                        throw new ConverterException(MessageUtils.getMessage(..,argsfacesContext));
                    else if (type.equals())
                        throw new ConverterException(MessageUtils.getMessage(..,,argsfacesContext));
                    else if (type.equals())
                        throw new ConverterException(MessageUtils.getMessage(..,,argsfacesContext));
                    else
                        throw new ConverterException("invalid type '" +  + "'");
                }
            }
        }
        return null;
    }
    public String getAsString(FacesContext facesContextUIComponent uiComponentObject value)
    {
        if (facesContext == nullthrow new NullPointerException("facesContext");
        if (uiComponent == nullthrow new NullPointerException("uiComponent");
        if (value == null)
        {
            return "";
        }
        if (value instanceof String)
        {
            return (String)value;
        }
        DateFormat format = getDateFormat();
        TimeZone tz = getTimeZone(); 
        if (tz != null)
        {
            format.setTimeZone(tz);
        }
        try
        {
            return format.format(value);
        }
        catch (Exception e)
        {
            throw new ConverterException(MessageUtils.getMessage(..
                    new Object[]{value,MessageUtils.getLabel(facesContextuiComponent)}, facesContext)
                    ,e);
        }
    }
    private DateFormat getDateFormat()
    {
        String type = getType();
        DateFormat format;
        if ( != null)
        {
            try 
            {
                format = new SimpleDateFormat(getLocale());
            } 
                catch (IllegalArgumentException iae)
            {
                throw new ConverterException("Invalid pattern"iae);    
            }
        }
        else if (type.equals())
        {
            format = DateFormat.getDateInstance(calcStyle(getDateStyle()), getLocale());
        }
        else if (type.equals())
        {
            format = DateFormat.getTimeInstance(calcStyle(getTimeStyle()), getLocale());
        }
        else if (type.equals())
        {
            format = DateFormat.getDateTimeInstance(calcStyle(getDateStyle()),
                                                    calcStyle(getTimeStyle()),
                                                    getLocale());
        }
        else
        {
            throw new ConverterException("invalid type '" +  + "'");
        }
        
        // format cannot be lenient (JSR-127)
        format.setLenient(false);
        return format;
    }
    private int calcStyle(String name)
    {
        if (name.equals())
        {
            return .;
        }
        if (name.equals())
        {
            return .;
        }
        if (name.equals())
        {
            return .;
        }
        if (name.equals())
        {
            return .;
        }
        if (name.equals())
        {
            return .;
        }
        throw new ConverterException("invalid style '" + name + "'");
    }
    // STATE SAVE/RESTORE
    public void restoreState(FacesContext facesContextObject state)
    {
        if (state != null)
        {
            Object[] values = (Object[])state;
             = (String)values[0];
             = (Locale)values[1];
             = (String)values[2];
             = (String)values[3];
             = (TimeZone)values[4];
             = (String)values[5];
            restoreValueExpressionMap(facesContextvalues[6]);
        }
    }
    public Object saveState(FacesContext facesContext)
    {
        if (!initialStateMarked())
        {
            Object[] values = new Object[7];
            values[0] = ;
            values[1] = ;
            values[2] = ;
            values[3] = ;
            values[4] = ;
            values[5] = ;
            values[6] = saveValueExpressionMap(facesContext);
            return values;
        }
        return null;
    }
    
    // --------------------- borrowed from UIComponentBase ------------
    private Map _valueExpressionMap = null;
    {
        if (name == nullthrow new NullPointerException("name");
        if ( == null)
        {
            return null;
        }
        else
        {
            return (ValueExpression).get(name);
        }
    }
    public void setValueExpression(String name,
                                ValueExpression binding)
    {
        if (name == nullthrow new NullPointerException("name");
        if ( == null)
        {
             = new HashMap();
        }
        .put(namebinding);
        clearInitialState();
    }
    private Object saveValueExpressionMap(FacesContext context)
    {
        if ( != null)
        {
            int initCapacity = (.size() * 4 + 3) / 3;
            HashMap stateMap = new HashMap(initCapacity);
            for (Iterator it = .entrySet().iterator(); it.hasNext(); )
            {
                Map.Entry entry = (Map.Entry)it.next();
                stateMap.put(entry.getKey(),
                             ConverterBase.saveAttachedState(contextentry.getValue()));
            }
            return stateMap;
        }
        else
        {
            return null;
        }
    }
    private void restoreValueExpressionMap(FacesContext contextObject stateObj)
    {
        if (stateObj != null)
        {
            Map stateMap = (Map)stateObj;
            int initCapacity = (stateMap.size() * 4 + 3) / 3;
             = new HashMap(initCapacity);
            for (Iterator it = stateMap.entrySet().iterator(); it.hasNext(); )
            {
                Map.Entry entry = (Map.Entry)it.next();
                .put(entry.getKey(),
                        ConverterBase.restoreAttachedState(contextentry.getValue()));
            }
        }
        else
        {
             = null;
        }
    }
    protected FacesContext getFacesContext()
    {
        if ( == null)
        {
            return FacesContext.getCurrentInstance();
        }
        else
        {
            return ;
        }
    }
    
    boolean isCachedFacesContext()
    {
        return  != null;
    }
    
    void setCachedFacesContext(FacesContext facesContext)
    {
         = facesContext;
    }
    
    // GETTER & SETTER
    
    
The style of the date. Values include: default, short, medium, long, and full.
    @JSFProperty(inheritedTag = false)
    public String getDateStyle()
    {
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("dateStyle");
        if (vb != null)
        {
            return (Stringvb.getValue(getFacesContext().getELContext());
        }
        return ;
    }
    public void setDateStyle(String dateStyle)
    {
         = dateStyle;
        clearInitialState();
    }

    
The name of the locale to be used, instead of the default.
    @JSFProperty(inheritedTag = false,deferredValueType="java.lang.Object")
    public Locale getLocale()
    {        
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("locale");
        if (vb != null)
        {
            Object _localeValue = vb.getValue(getFacesContext().getELContext());
            if (_localeValue instanceof String)
            {
                _localeValue = org.apache.myfaces.commons.util.TagUtils.getLocale((String)_localeValue);
            }
            return (java.util.Locale)_localeValue;
        }
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getViewRoot().getLocale();
    }
    public void setLocale(Locale locale)
    {
         = locale;
        clearInitialState();
    }

    
A custom Date formatting pattern, in the format used by java.text.SimpleDateFormat.
    @JSFProperty(inheritedTag = false)
    public String getPattern()
    {
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("pattern");
        if (vb != null)
        {
            return (Stringvb.getValue(getFacesContext().getELContext()).toString();
        }
        return null;
    }
    public void setPattern(String pattern)
    {
         = pattern;
        clearInitialState();
    }

    
The style of the time. Values include: default, short, medium, long, and full.
    @JSFProperty(inheritedTag = false)
    public String getTimeStyle()
    {
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("timeStyle");
        if (vb != null)
        {
            return (Stringvb.getValue(getFacesContext().getELContext()).toString();
        }
        return ;
    }
    public void setTimeStyle(String timeStyle)
    {
         = timeStyle;
        clearInitialState();
    }

    
The time zone to use instead of GMT (the default timezone). When this value is a value-binding to a TimeZone instance, that timezone is used. Otherwise this value is treated as a String containing a timezone id, ie as the ID parameter of method java.util.TimeZone.getTimeZone(String).
    @JSFProperty(inheritedTag = false,deferredValueType="java.lang.Object")
    public TimeZone getTimeZone()
    {
        //return _timeZone != null ? _timeZone : TIMEZONE_DEFAULT;
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("timeZone");
        if (vb != null)
        {
            Object _timeZoneValue = vb.getValue(getFacesContext().getELContext());
            if(_timeZoneValue instanceof java.util.TimeZone)
            {
                return (java.util.TimeZone_timeZoneValue;
            }
            else
            {
                return java.util.TimeZone.getTimeZone(_timeZoneValue.toString());
            }
        }
        return TimeZone.getDefault(); //TIMEZONE_DEFAULT
    }
    public void setTimeZone(TimeZone timeZone)
    {
         = timeZone;
        clearInitialState();
    }

    
Specifies whether the date, time, or both should be parsed/formatted. Values include: date, time, and both. Default based on setting of timeStyle and dateStyle.
    @JSFProperty(inheritedTag = false)
    public String getType()
    {
        if ( != null)
        {
            return ;
        }
        ValueExpression vb = getValueExpression("type");
        if (vb != null)
        {
            return (Stringvb.getValue(getFacesContext().getELContext()).toString();
        }
        return ;
    }
    public void setType(String type)
    {
         = type;
        clearInitialState();
    }
New to GrepCode? Check out our FAQ X