View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: DateFieldValueConvertor.java,v $
31   * Revision 1.4  2005/04/11 12:27:01  colinmacleod
32   * Added preliminary support for filters.
33   * Added FieldValueConvertor factor interface
34   * to split off value convertors for reuse.
35   *
36   * Revision 1.3  2005/04/09 18:04:15  colinmacleod
37   * Changed copyright text to GPL v2 explicitly.
38   *
39   * Revision 1.2  2005/01/06 22:13:21  colinmacleod
40   * Moved up a version number.
41   * Changed copyright notices to 2005.
42   * Updated the documentation:
43   *   - started working on multiproject:site docu.
44   *   - changed the logo.
45   * Added checkstyle and fixed LOADS of style issues.
46   * Added separate thirdparty subproject.
47   * Added struts (in web), util and webgui (in webtheme) from ivata op.
48   *
49   * Revision 1.1  2004/12/29 20:07:06  colinmacleod
50   * Renamed subproject masks to mask.
51   *
52   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
53   * Ready for 0.1 release
54   * -----------------------------------------------------------------------------
55   */
56  package com.ivata.mask.field.date;
57  import java.text.DateFormat;
58  import java.text.ParseException;
59  import java.text.SimpleDateFormat;
60  import java.util.Date;
61  import com.ivata.mask.field.FieldValueConvertor;
62  /***
63   * <p>
64   * Convert an object representing a date to a string.
65   * </p>
66   *
67   * @since ivata masks 0.1 (2004-04-14)
68   * @author Colin MacLeod
69   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
70   * @version $Revision: 1.4 $
71   */
72  public class DateFieldValueConvertor extends FieldValueConvertor {
73      /***
74       * <p>
75       * This format does all the hard work!
76       * </p>
77       */
78      private DateFormat format;
79      /***
80       * <p>
81       * Construct a new value convertor from the format pattern supplied.
82       * </p>
83       *
84       * @param pattern
85       *            number format pattern to use to convert the number. See
86       *            {@link java.text.SimpleDateFormat}.
87       */
88      public DateFieldValueConvertor(final String pattern) {
89          format = new SimpleDateFormat(pattern);
90      }
91      /***
92       * <p>
93       * Convert an object representing a date to a string.
94       * </p>
95       *
96       * @param objectValue
97       *            object to be converted.
98       * @return string equivalent of the object provided.
99       */
100     protected String toString(final Object objectValue) {
101         if (objectValue == null) {
102             return "";
103         }
104         Date date = (Date) objectValue;
105         return format.format(date);
106     }
107     /***
108      * <p>
109      * Convert a date value from a string.
110      * </p>
111      *
112      * @param propertyClass
113      *            exact class to be converted to.
114      * @param stringValue
115      *            value to be converted.
116      * @return valid object value converted from a string.
117      * @see com.ivata.mask.field.FieldValueConvertor#convertFromString
118      */
119     public Object convertFromString(final Class propertyClass,
120             final String stringValue) {
121         try {
122             return format.parse(stringValue);
123         } catch (ParseException e) {
124             throw new FieldValueException(e);
125         }
126     }
127 }