KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > attribute > DateAttribute


1 package org.tigris.scarab.attribute;
2
3 import java.text.ParseException JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import org.apache.torque.TorqueException;
9 import org.tigris.scarab.om.AttributeValue;
10
11 /* ================================================================
12  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement: "This product includes
27  * software developed by Collab.Net <http://www.Collab.Net/>."
28  * Alternately, this acknowlegement may appear in the software itself, if
29  * and wherever such third-party acknowlegements normally appear.
30  *
31  * 4. The hosted project names must not be used to endorse or promote
32  * products derived from this software without prior written
33  * permission. For written permission, please contact info@collab.net.
34  *
35  * 5. Products derived from this software may not use the "Tigris" or
36  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
37  * prior written permission of Collab.Net.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
43  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
45  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
47  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of Collab.Net.
55  */

56
57 /**
58  *
59  * @author <a HREF="mailto:fedor.karpelevitch@home.com">Fedor</a>
60  * @version $Revision: 9715 $ $Date: 2005-06-04 12:52:06 +0200 (Sat, 04 Jun 2005) $
61  */

62 public class DateAttribute extends StringAttribute
63 {
64     private static SimpleDateFormat JavaDoc internalFormat = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssSS");
65     
66     /**
67      * Receives the value in yyyyMMddHHmmssSS format and returns it
68      * formatted according to the mask parameter.
69      * If the value is not parseable, it will be returned unchanged.
70      * @param value
71      * @param mask
72      * @return
73      */

74     public static String JavaDoc dateFormat(String JavaDoc value, String JavaDoc mask)
75     {
76         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(mask);
77         String JavaDoc val = value;
78         try
79         {
80             if (val == null)
81                 val = "";
82             else
83                 val = sdf.format(internalFormat.parse(value));
84         }
85         catch (ParseException JavaDoc e)
86         {
87             // Will return the same value
88
}
89         return val;
90     }
91     /**
92      * Receives the value in the format defined bu 'mask' and
93      * returns it formatted in internal (yyyyMMddHHmmssSS) format.
94      * If the value is not parseable, it will be returned unchanged.
95      * @param value
96      * @param mask
97      * @return
98      */

99     public static String JavaDoc internalDateFormat(String JavaDoc value, String JavaDoc mask)
100     {
101         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(mask);
102         String JavaDoc val = value;
103         try
104         {
105             if (val == null)
106                 val = "";
107             else
108                 val = internalFormat.format(sdf.parse(value));
109         }
110         catch (ParseException JavaDoc e)
111         {
112             // Will return the same value
113
}
114         return val;
115     }
116     
117     /**
118      * Utility method that will convert every DateAttribute in a list from the user's
119      * locale format to the internal (yyyyMMddHHmmssSS) format.
120      * @param issue
121      * @param mask
122      * @throws TorqueException
123      */

124     public static void convertDateAttributes(Collection JavaDoc attributeValues, String JavaDoc mask) throws TorqueException
125     {
126         for (Iterator JavaDoc iter = attributeValues.iterator(); iter.hasNext(); )
127         {
128             AttributeValue av = (AttributeValue)iter.next();
129             if (av instanceof DateAttribute)
130             {
131                 av.setValue(internalDateFormat(av.getValue(), mask));
132             }
133         }
134     }
135 }
136
Popular Tags