KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > util > xml > DateFormat


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.util.xml;
11
12 import org.apache.xpath.XPathAPI;
13 import java.util.*;
14
15 import org.mmbase.bridge.*;
16
17 import org.mmbase.util.logging.Logger;
18 import org.mmbase.util.logging.Logging;
19
20 /**
21  * Dates are stored as integer in mmbase. If you want to show these dates
22  * in a nice format in the XSL transformation.
23  * it is necessary to use this Xalan extension.
24  * The XSLT looks like this then:
25  *
26  * <pre>
27  * &lt;xsl:stylesheet version = "1.0"
28  * xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
29  * xmlns:date ="org.mmbase.bridge.util.xml.DateFormat"
30  * &gt;
31  * </pre>
32  *
33  * @author Nico Klasens
34  * @author Martijn Houtman
35  * @author Michiel Meeuwissen
36  * @version $Id: DateFormat.java,v 1.14 2005/10/07 18:47:41 michiel Exp $
37  * @since MMBase-1.7
38  */

39 public class DateFormat {
40
41     private static final Logger log = Logging.getLoggerInstance(DateFormat.class);
42
43     /**
44      * TimeZone null or blank means server timezone. If timeZone is not
45      * recognized it will fall back to GMT.
46      * @param timeZone String representation of a timezine
47      * @return TimeZone object or the default timezone when id does not exist
48      */

49     private static TimeZone getTimeZone(String JavaDoc timeZone) {
50         if (timeZone == null || "".equals(timeZone.trim())) {
51             return TimeZone.getDefault();
52         } else {
53             TimeZone tz = TimeZone.getTimeZone(timeZone);
54             if (!tz.getID().equals(timeZone)) {
55                 tz = TimeZone.getDefault();
56             }
57             return tz;
58         }
59     }
60
61     /**
62      * Formats a node's field value with the date pattern
63      * @param number the number or alias of the node containing the field
64      * @param fieldName the name of the field to format
65      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
66      * @param timeZone timezone of the field value
67      * @param language Language of the field value
68      * @param country country of the field value
69      * @return the formatted string
70      */

71     public static String JavaDoc format(String JavaDoc number, String JavaDoc fieldName, String JavaDoc pattern, String JavaDoc timeZone, String JavaDoc language, String JavaDoc country) {
72         return format("mmbase", number, fieldName, pattern, timeZone, language, country);
73     }
74
75
76     /**
77      * Formats a node's field value with the date pattern
78      * @param cloudName the name of the cloud in which to find the node
79      * @param number the number or alias of the node containing the field
80      * @param fieldName the name of the field to format
81      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
82      * @param timeZone timezone of the field value
83      * @param language Language of the field value
84      * @param country country of the field value
85      * @return the formatted string
86      */

87     public static String JavaDoc format(String JavaDoc cloudName, String JavaDoc number, String JavaDoc fieldName, String JavaDoc pattern, String JavaDoc timeZone, String JavaDoc language, String JavaDoc country) {
88         try {
89             Cloud cloud = LocalContext.getCloudContext().getCloud(cloudName);
90             cloud.setLocale(new Locale(language, country));
91             return format(cloud, number, fieldName, pattern, timeZone);
92         } catch (BridgeException e) {
93             return "could not find '" + fieldName + "' on node '" + number + "' (" + e.toString() + ")";
94         }
95     }
96
97
98     /**
99      * Formats a node's field value with the date pattern
100      * @param cloud the cloud in which to find the node
101      * @param number the number or alias of the node containing the field
102      * @param fieldName the name of the field to format
103      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
104      * @param timeZone timezone of the field value
105      * @return the formatted string
106      */

107     public static String JavaDoc format(Cloud cloud, String JavaDoc number, String JavaDoc fieldName, String JavaDoc pattern, String JavaDoc timeZone) {
108         try {
109             Node node = cloud.getNode(number);
110             String JavaDoc fieldValue = "" + node.getIntValue(fieldName);
111             return format(fieldValue, pattern, 1000, timeZone, cloud.getLocale());
112         } catch (BridgeException e) {
113             if (log.isDebugEnabled()) {
114                 log.debug("could not find '" + fieldName + "' on node '" + number + "'");
115                 log.trace(Logging.stackTrace(e));
116             }
117             return "could not find " + fieldName + " on node " + number + "(" + e.toString() + ")";
118         }
119     }
120
121     /**
122      * Formats the fieldvalue to a date pattern
123      *
124      * @param fieldValue time-stamp in seconds
125      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
126      * @param timeZone timezone of the field value
127      * @param language Language of the field value
128      * @param country country of the field value
129      * @return the formatted string
130      */

131     public static String JavaDoc format(String JavaDoc fieldValue, String JavaDoc pattern, String JavaDoc timeZone, String JavaDoc language, String JavaDoc country) {
132         return format(fieldValue, pattern, 1000, timeZone, language, country);
133     }
134
135     /** Formats the fieldvalue to a date pattern
136      *
137      * @param fieldValue time-stamp
138      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
139      * @param factor Factor to multiply fieldvalue to make milliseconds. Should be 1000 normally (so field in seconds)
140      * @param timeZone Timezone. Null or blank means server timezone. If not recognized it will fall back to GMT.
141      * @param language The language for which the date must be formatted.
142      * @param country The country for which the date must be formatted.
143      * @return the formatted string
144      */

145     public static String JavaDoc format(String JavaDoc fieldValue, String JavaDoc pattern, int factor, String JavaDoc timeZone, String JavaDoc language, String JavaDoc country) {
146         return format(fieldValue, pattern, factor, timeZone, new Locale(language, country));
147     }
148
149     /**
150      * @param fieldValue time-stamp
151      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
152      * @param factor Factor to multiply fieldvalue to make milliseconds. Should be 1000 normally (so field in seconds)
153      * @param timeZone Timezone. Null or blank means server timezone. If not recognized it will fall back to GMT.
154      * @param locale The locale for which the date must be formatted.
155      * @return the formatted String
156      */

157     protected static String JavaDoc format(String JavaDoc fieldValue, String JavaDoc pattern, int factor, String JavaDoc timeZone, Locale locale) {
158         if (fieldValue == null || "".equals(fieldValue)) {
159            return "";
160         }
161         java.text.DateFormat JavaDoc df = org.mmbase.util.DateFormats.getInstance(pattern, timeZone, locale);
162         long seconds = Long.valueOf(fieldValue).longValue();
163         return df.format(new Date(seconds * factor));
164     }
165
166     /**
167      * Formats a node's field value with the date pattern.
168      * This version requires you to supply a DOM node. It will search for a tag of the form
169      * &lt;field name='number' &gt; and uses it's contents to retrieve the node.
170      * @deprecated not sure where this is used?
171      * @param cloud the cloud in which to find the node
172      * @param node A DOM node (xml) containing the node's fields as subtags
173      * @param fieldName the name of the field to format
174      * @param pattern the date pattern (i.e. 'dd-MM-yyyy')
175      * @param timeZone The timezone of the field value
176      * @return the formatted string
177      * @throws javax.xml.transform.TransformerException if something went wrong while searching the DOM Node
178      */

179     public static String JavaDoc format(Cloud cloud, org.w3c.dom.Node JavaDoc node, String JavaDoc fieldName, String JavaDoc pattern, String JavaDoc timeZone) throws javax.xml.transform.TransformerException JavaDoc {
180         log.debug("calling with dom node");
181         // bit of a waste to use an xpath here?
182
String JavaDoc number = XPathAPI.eval(node, "./field[@name='number']").toString();
183         return format(cloud, number, fieldName, pattern, timeZone);
184     }
185
186     /** Returns the year part of the date
187      *
188      * @param fieldValue time-stamp
189      * @return year part
190      */

191     public static int getYear(String JavaDoc fieldValue) {
192         return getDatePart(fieldValue, 1000, Calendar.YEAR, "");
193     }
194
195     /** Returns the month part of the date
196      *
197      * @param fieldValue time-stamp
198      * @return month part
199      */

200     public static int getMonth(String JavaDoc fieldValue) {
201         return getDatePart(fieldValue, 1000, Calendar.MONTH, "") + 1;
202     }
203     
204     /** Returns the day of the month part of the date
205      *
206      * @param fieldValue time-stamp
207      * @return day of the month part
208      */

209     public static int getDay(String JavaDoc fieldValue) {
210         return getDatePart(fieldValue, 1000, Calendar.DAY_OF_MONTH, "");
211     }
212     
213     /** Returns the hours part of the date
214      *
215      * @param fieldValue time-stamp
216      * @return hours part
217      */

218     public static int getHours(String JavaDoc fieldValue) {
219         return getDatePart(fieldValue, 1000, Calendar.HOUR_OF_DAY, "");
220     }
221     
222     /** Returns the minutes part of the date
223      *
224      * @param fieldValue time-stamp
225      * @return minutes part
226      */

227     public static int getMinutes(String JavaDoc fieldValue) {
228         return getDatePart(fieldValue, 1000, Calendar.MINUTE, "");
229     }
230     
231     /** Returns the seconds part of the date
232      *
233      * @param fieldValue time-stamp
234      * @return seconds part
235      */

236     public static int getSeconds(String JavaDoc fieldValue) {
237         return getDatePart(fieldValue, 1000, Calendar.SECOND, "");
238     }
239
240     /** Returns the year part of the date
241      *
242      * @param fieldValue time-stamp
243      * @param timeZone timezone
244      * @return year part
245      */

246     public static int getYear(String JavaDoc fieldValue, String JavaDoc timeZone) {
247         return getDatePart(fieldValue, 1000, Calendar.YEAR, timeZone);
248     }
249
250     /** Returns the month part of the date
251      *
252      * @param fieldValue time-stamp
253      * @param timeZone timezone
254      * @return month part
255      */

256     public static int getMonth(String JavaDoc fieldValue, String JavaDoc timeZone) {
257         return getDatePart(fieldValue, 1000, Calendar.MONTH, timeZone) + 1;
258     }
259     
260     /** Returns the day of the month part of the date
261      *
262      * @param fieldValue time-stamp
263      * @param timeZone timezone
264      * @return day of the month part
265      */

266     public static int getDay(String JavaDoc fieldValue, String JavaDoc timeZone) {
267         return getDatePart(fieldValue, 1000, Calendar.DAY_OF_MONTH, timeZone);
268     }
269     
270     /** Returns the hours part of the date
271      *
272      * @param fieldValue time-stamp
273      * @param timeZone timezone
274      * @return hours part
275      */

276     public static int getHours(String JavaDoc fieldValue, String JavaDoc timeZone) {
277         return getDatePart(fieldValue, 1000, Calendar.HOUR_OF_DAY, timeZone);
278     }
279     
280     /** Returns the minutes part of the date
281      *
282      * @param fieldValue time-stamp
283      * @param timeZone timezone
284      * @return minutes part
285      */

286     public static int getMinutes(String JavaDoc fieldValue, String JavaDoc timeZone) {
287         return getDatePart(fieldValue, 1000, Calendar.MINUTE, timeZone);
288     }
289     
290     /** Returns the seconds part of the date
291      *
292      * @param fieldValue time-stamp
293      * @param timeZone timezone
294      * @return seconds part
295      */

296     public static int getSeconds(String JavaDoc fieldValue, String JavaDoc timeZone) {
297         return getDatePart(fieldValue, 1000, Calendar.SECOND, timeZone);
298     }
299
300     /** Returns the a part of the date
301      *
302      * @param fieldValue time-stamp
303      * @param factor Factor to multiply fieldvalue to make milliseconds. Should be 1000 normally (so field in seconds)
304      * @param datePart which part of the date should be returned. These are Calendar constants
305      * @return a part
306      */

307     public static int getDatePart(String JavaDoc fieldValue, int factor, int datePart) {
308         return getDatePart(fieldValue, factor, datePart, "");
309     }
310
311     /** Returns the a part of the date
312      *
313      * @param fieldValue time-stamp
314      * @param factor Factor to multiply fieldvalue to make milliseconds. Should be 1000 normally (so field in seconds)
315      * @param datePart which part of the date should be returned. These are Calendar constants
316      * @param timeZone Timezone. Null or blank means server timezone. If not recognized it will fall back to GMT.
317      * @return a part
318      */

319     public static int getDatePart(String JavaDoc fieldValue, int factor, int datePart, String JavaDoc timeZone) {
320         if (fieldValue != null && !"".equals(fieldValue.trim())) {
321             Calendar cal = Calendar.getInstance(getTimeZone(timeZone));
322             long seconds = Long.valueOf(fieldValue).longValue();
323             cal.setTimeInMillis(seconds * factor);
324             return cal.get(datePart);
325         } else {
326             return -1;
327         }
328     }
329
330 }
331
Popular Tags