KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > DateFormats


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.util;
11
12 import java.util.*;
13 import java.text.*;
14
15 import org.mmbase.util.logging.Logger;
16 import org.mmbase.util.logging.Logging;
17
18 /**
19  * Utility function to create DateFormat instances.
20  *
21  * @author Michiel Meeuwissen
22  * @since MMBase-1.7.1
23  * @version $Id: DateFormats.java,v 1.4 2006/02/16 14:35:59 andre Exp $
24  */

25 public class DateFormats {
26
27     private static final Logger log = Logging.getLoggerInstance(DateFormats.class);
28
29     /**
30      * Creates a DateFormat instance, based on a String.
31      *
32      * @param format The format defining the DateFormat. This can be constants like :FULL, :FULL.FULL, :LONG, :MEDIUM or :SHORT.
33      * It can also be 'e' for weekday. Also 'RFC822' or 'rfc822' is possible then the
34      * quite complicated http://www.faqs.org/rfcs/rfc822.html compliant date and time
35      * is made, which comes in handy when you need to create a rss feed f.e.
36      * Or none of those, then a SimpleDateFormat is instantiated.
37      * @param timeZone A String describing the timeZone (see DateFormat#setTimeZone)
38      * @param locale Most DateFormat's need a Locale too.
39      * @throws IllegalArgumentException
40      */

41     public static DateFormat getInstance(String JavaDoc format, String JavaDoc timeZone, Locale locale) {
42         DateFormat df;
43         if (format.length() > 0 && format.charAt(0) == ':') {
44             log.debug("found symbolic format");
45             if (format.charAt(1) == '.') {
46                 df = DateFormat.getTimeInstance(getDateFormatStyle(format.substring(2)), locale);
47             } else if (format.indexOf('.') == -1) {
48                 df = DateFormat.getDateInstance(getDateFormatStyle(format.substring(1)), locale);
49             } else {
50                 int i = format.indexOf('.');
51                 df = DateFormat.getDateTimeInstance(getDateFormatStyle(format.substring(1, i)),
52                                                             getDateFormatStyle(format.substring(i+1)), locale);
53             }
54         } else if (format.equals("e")) {
55             df = new DayOfWeekDateFormat();
56         } else if (format.equals("RFC822") || format.equals("rfc822")) {
57             df = new SimpleDateFormat("EE, dd MMM yyyy hh:mm:ss Z", Locale.US);
58         } else {
59             df = new SimpleDateFormat(format, locale);
60         }
61         if (!( timeZone == null || timeZone.equals(""))) {
62             df.setTimeZone(TimeZone.getTimeZone(timeZone));
63         } else {
64             df.setTimeZone(org.mmbase.util.dateparser.DateParser.defaultTimeZone);
65         }
66         return df;
67
68     }
69
70     /**
71      * Converts a string to a DateFormat constant.
72      *
73      * @param style A string describing the dateformat style (FULL, LONG, MEDIUM, SHORT)
74      * @return A DateFormat style constant.
75      * @see java.text.DateFormat
76      */

77     private static int getDateFormatStyle(String JavaDoc style) {
78         if ("FULL".equals(style)) {
79             return DateFormat.FULL;
80         } else if ("LONG".equals(style)) {
81             return DateFormat.LONG;
82         } else if ("MEDIUM".equals(style)) {
83             return DateFormat.MEDIUM;
84         } else if ("SHORT".equals(style)) {
85             return DateFormat.SHORT;
86         } else {
87             throw new IllegalArgumentException JavaDoc("Unknown DateFormat Style " + style);
88         }
89     }
90
91     /**
92      * There is no DateFormat which can return the day of the week as a number available in
93      * java.text package. This provides one.
94      */

95
96     protected static class DayOfWeekDateFormat extends DateFormat {
97         private TimeZone zone = null;
98         public Date parse(String JavaDoc source, ParsePosition pos) {
99             Calendar calendar = Calendar.getInstance(zone != null ? zone : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
100             int day = source.charAt(0) - '0';
101             pos.setIndex(pos.getIndex() + 1);
102             calendar.set(Calendar.DAY_OF_WEEK, day);
103             return calendar.getTime();
104         }
105         public StringBuffer JavaDoc format(Date date, StringBuffer JavaDoc toAppendTo, FieldPosition pos) {
106             Calendar calendar = Calendar.getInstance(zone != null ? zone : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
107             calendar.setTime(date);
108             // pos.setBeginIndex(0); pos.setEndIndex(1);
109
toAppendTo.append(calendar.get(Calendar.DAY_OF_WEEK));
110             return toAppendTo;
111         }
112
113         public void setTimeZone(TimeZone value) {
114             zone = value;
115         }
116
117         
118     }
119 }
120
Popular Tags