KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > l10n > DateFormatter


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Dec 23, 2004
33  */

34 package com.nightlabs.l10n;
35
36 import java.text.DateFormat JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.Locale JavaDoc;
39
40 import com.nightlabs.config.Config;
41 import com.nightlabs.config.ConfigException;
42
43 /**
44  * @author Marco Schulze - marco at nightlabs dot de
45  */

46 public class DateFormatter
47 {
48     private static DateFormatter _sharedInstance = null;
49
50     /**
51      * In case there is no shared instance existing yet, it will be
52      * created with the shared instance of Config. Hence, this method
53      * fails if there is no shared instance of Config.
54      *
55      * @return Returns the shared instance of DateFormatter.
56      */

57     public static DateFormatter sharedInstance()
58     {
59         if (_sharedInstance == null)
60             _sharedInstance = new DateFormatter(Config.sharedInstance(), Locale.getDefault());
61
62         return _sharedInstance;
63     }
64
65     protected Config config;
66     protected Locale JavaDoc locale;
67
68     /**
69      * @param config
70      */

71     public DateFormatter(Config config, Locale JavaDoc locale)
72     {
73         this.config = config;
74         this.locale = locale;
75         try {
76             config.createConfigModule(GlobalL10nSettings.class);
77         } catch (ConfigException e) {
78             throw new RuntimeException JavaDoc(e);
79         }
80     }
81
82     private DateFormatProvider dateFormatProvider = null;
83
84     public DateFormatProvider getDateFormatProvider()
85     {
86         if (dateFormatProvider != null)
87             return dateFormatProvider;
88         
89         L10nFormatCfMod l10nFormatCfMod;
90         try {
91             l10nFormatCfMod = (L10nFormatCfMod) ConfigUtil.createConfigModule(
92                     config, L10nFormatCfMod.class, locale.getLanguage(), locale.getCountry());
93         
94             String JavaDoc className = l10nFormatCfMod.getDateFormatProvider();
95         
96             Class JavaDoc clazz = Class.forName(className);
97             if (!DateFormatProvider.class.isAssignableFrom(clazz))
98                 throw new ClassCastException JavaDoc("class \""+className+"\" defined in config module \""+L10nFormatCfMod.class.getName()+"\" does not implement interface \""+DateFormatProvider.class.getName()+"\"!");
99
100             DateFormatProvider dfp = (DateFormatProvider) clazz.newInstance();
101             dfp.init(config, locale.getLanguage(), locale.getCountry());
102
103             this.dateFormatProvider = dfp;
104             return dateFormatProvider;
105         } catch (RuntimeException JavaDoc e) {
106             throw e;
107         } catch (Exception JavaDoc e) {
108             throw new RuntimeException JavaDoc(e);
109         }
110     }
111     
112     public static String JavaDoc formatDateShortTimeHM(Date JavaDoc dt, boolean weekDay)
113     {
114         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
115                         DateFormatProvider.DATE_SHORT | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0)
116                         | DateFormatProvider.TIME);
117         return dateFormat.format(dt);
118     }
119     
120     public static String JavaDoc formatDateLongTimeHM(Date JavaDoc dt, boolean weekDay)
121     {
122         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
123                         DateFormatProvider.DATE_LONG | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0)
124                         | DateFormatProvider.TIME);
125         return dateFormat.format(dt);
126     }
127
128     public static String JavaDoc formatDateShortTimeHMS(Date JavaDoc dt, boolean weekDay)
129     {
130         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
131                         DateFormatProvider.DATE_SHORT | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0)
132                         | DateFormatProvider.TIME_SEC);
133         return dateFormat.format(dt);
134     }
135     
136     public static String JavaDoc formatDateLongTimeHMS(Date JavaDoc dt, boolean weekDay)
137     {
138         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
139                         DateFormatProvider.DATE_LONG | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0)
140                         | DateFormatProvider.TIME_SEC);
141         return dateFormat.format(dt);
142     }
143
144
145     /**
146      * Produces sth. like 2004-12-24 (means only digits and no
147      * week day).
148      */

149     public static String JavaDoc formatDateShort(Date JavaDoc dt, boolean weekDay)
150     {
151         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
152                         DateFormatProvider.DATE_SHORT | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0));
153         return dateFormat.format(dt);
154     }
155
156     /**
157      * Produces sth. like 2004 December 24
158      */

159     public static String JavaDoc formatDateLong(Date JavaDoc dt, boolean weekDay)
160     {
161         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
162                 DateFormatProvider.DATE_LONG | (weekDay ? DateFormatProvider.DATE_WEEKDAY : 0));
163         return dateFormat.format(dt);
164     }
165
166     /**
167      * Produces sth. like 23:51
168      */

169     public static String JavaDoc formatTimeHM(Date JavaDoc dt)
170     {
171         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
172                 DateFormatProvider.TIME);
173         return dateFormat.format(dt);
174     }
175
176     /**
177      * Produces sth. like 23:51:47
178      */

179     public static String JavaDoc formatTimeHMS(Date JavaDoc dt)
180     {
181         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
182                 DateFormatProvider.TIME_SEC);
183         return dateFormat.format(dt);
184     }
185
186     /**
187      * Produces sth. like 23:51:47
188      */

189     public static String JavaDoc formatTimeHMSmsec(Date JavaDoc dt)
190     {
191         DateFormat JavaDoc dateFormat = sharedInstance().getDateFormatProvider().getDateFormat(
192                 DateFormatProvider.TIME_MSEC);
193         return dateFormat.format(dt);
194     }
195 }
Popular Tags