nbubna      2003/12/05 21:45:25

  Modified:    src/java/org/apache/velocity/tools/generic DateTool.java
  Log:
  fill out and clean up the toDate(...) methods
  
  Revision  Changes    Path
  1.9       +51 -45    
jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/DateTool.java
  
  Index: DateTool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/DateTool.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DateTool.java     11 Nov 2003 01:55:21 -0000      1.8
  +++ DateTool.java     6 Dec 2003 05:45:25 -0000       1.9
  @@ -617,9 +617,11 @@
       // ------------------------- date conversion methods ---------------
   
       /**
  -     * Converts an object to an instance of [EMAIL PROTECTED] Date}. Uses a 
  -     * DateFormat to parse the string value of the object if it is not
  -     * an instance of Date or Calendar or Long.
  +     * Converts an object to an instance of [EMAIL PROTECTED] Date} using the
  +     * format returned by [EMAIL PROTECTED] getFormat()},the [EMAIL PROTECTED] 
Locale} returned 
  +     * by [EMAIL PROTECTED] #getLocale()}, and the [EMAIL PROTECTED] TimeZone} 
returned by
  +     * [EMAIL PROTECTED] #getTimeZone()} if the object is not already an instance
  +     * of Date, Calendar, or Long.
        *
        * @param obj the date to convert
        * @return the object as a [EMAIL PROTECTED] Date} or <code>null</code> if no
  @@ -627,42 +629,15 @@
        */
       public Date toDate(Object obj)
       {
  -        if (obj == null)
  -        {
  -            return null;
  -        }
  -        if (obj instanceof Date)
  -        {
  -            return (Date)obj;
  -        }
  -        if (obj instanceof Calendar)
  -        {
  -            return ((Calendar)obj).getTime();
  -        }
  -        if (obj instanceof Long) 
  -        {
  -            Date d = new Date();
  -            d.setTime(((Long)obj).longValue());
  -            return d;
  -        }
  -        try
  -        {
  -            //TODO? add better parsing support?
  -            //try parsing the obj as String w/a DateFormat
  -            DateFormat parser = DateFormat.getInstance();
  -            return parser.parse(String.valueOf(obj));
  -        }
  -        catch (Exception e)
  -        {
  -            return null;
  -        }
  +        return toDate(getFormat(), obj, getLocale(), getTimeZone());
       }
   
       /**
        * Converts an object to an instance of [EMAIL PROTECTED] Date} using the
  -     * specified format and the [EMAIL PROTECTED] Locale} returned by 
  -     * [EMAIL PROTECTED] #getLocale()} if the object is not already an instance
  -     * of Date or Calendar.
  +     * specified format,the [EMAIL PROTECTED] Locale} returned by 
  +     * [EMAIL PROTECTED] #getLocale()}, and the [EMAIL PROTECTED] TimeZone} 
returned by
  +     * [EMAIL PROTECTED] #getTimeZone()} if the object is not already an instance
  +     * of Date, Calendar, or Long.
        *
        * @param format - the format the date is in
        * @param obj - the date to convert
  @@ -672,13 +647,13 @@
        */
       public Date toDate(String format, Object obj)
       {
  -        return toDate(format, obj, getLocale());
  +        return toDate(format, obj, getLocale(), getTimeZone());
       }
   
       /**
        * Converts an object to an instance of [EMAIL PROTECTED] Date} using the
  -     * specified format and [EMAIL PROTECTED] Locale}if the object is not already
  -     * an instance of Date or Calendar.
  +     * specified format and [EMAIL PROTECTED] Locale} if the object is not already
  +     * an instance of Date, Calendar, or Long.
        *
        * @param format - the format the date is in
        * @param obj - the date to convert
  @@ -689,17 +664,48 @@
        */
       public Date toDate(String format, Object obj, Locale locale)
       {
  -        //first try the easiest conversions
  -        Date date = toDate(obj);
  -        if (date != null)
  +        return toDate(format, obj, locale, getTimeZone());
  +    }
  +
  +    /**
  +     * Converts an object to an instance of [EMAIL PROTECTED] Date} using the
  +     * specified format, [EMAIL PROTECTED] Locale}, and [EMAIL PROTECTED] TimeZone} 
if the 
  +     * object is not already an instance of Date, Calendar, or Long.
  +     *
  +     * @param format - the format the date is in
  +     * @param obj - the date to convert
  +     * @param locale - the [EMAIL PROTECTED] Locale}
  +     * @param timezone - the [EMAIL PROTECTED] TimeZone}
  +     * @return the object as a [EMAIL PROTECTED] Date} or <code>null</code> if no
  +     *         conversion is possible
  +     * @see #getDateFormat
  +     * @see SimpleDateFormat#parse
  +     */
  +    public Date toDate(String format, Object obj, 
  +                       Locale locale, TimeZone timezone)
  +    {
  +        if (obj == null)
           {
  -            return date;
  +            return null;
  +        }
  +        if (obj instanceof Date)
  +        {
  +            return (Date)obj;
  +        }
  +        if (obj instanceof Calendar)
  +        {
  +            return ((Calendar)obj).getTime();
  +        }
  +        if (obj instanceof Long) 
  +        {
  +            Date d = new Date();
  +            d.setTime(((Long)obj).longValue());
  +            return d;
           }
           try
           {
  -            //TODO? add better parsing support?
               //try parsing w/a customized SimpleDateFormat
  -            SimpleDateFormat parser = new SimpleDateFormat(format, locale);
  +            DateFormat parser = getDateFormat(format, locale, timezone);
               return parser.parse(String.valueOf(obj));
           }
           catch (Exception e)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to