View Javadoc

1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.i18n;
30  
31  import java.text.DateFormat;
32  import java.text.SimpleDateFormat;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.Calendar;
36  import java.util.Date;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.TimeZone;
40  
41  /**
42   * The DateFormat contex tool.
43   * 
44   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
45   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
46   * @version $Id: DateFormatTool.java,v 1.8 2005/03/22 11:11:35 pablo Exp $
47   */
48  public class DateFormatTool
49  {
50      /*** The date formater. */
51      protected DateFormatter dateFormater;
52      
53  	/*** The current locale. */
54  	protected Locale locale;
55  
56      /*** The DateFormat in use. */
57      protected DateFormat format;
58  
59  	/***
60  	 * Default constructor.
61       * 
62  	 * @param dateFormater the date formatter object.
63  	 * @param locale the locale.
64       * @param format the date format object.
65  	 */
66  	public DateFormatTool(DateFormatter dateFormater, Locale locale, DateFormat format)
67  	{
68  		this.dateFormater = dateFormater;
69  		this.locale = locale;
70  		this.format = format;
71  	}
72  	
73      // public API ////////////////////////////////////////////////////////////
74  	
75      /***
76       * Sets the formatting style.
77       *
78       * @param patternAlias the pattern name.
79       * @return new DateFormatTool instance.
80       * @throws Exception if the specified pattern alias is not configured for this tool.
81       */
82      public DateFormatTool style(String patternAlias)
83          throws Exception
84      {
85          DateFormatTool target = createInstance(this);
86          target.format = dateFormater.getDateFormat(patternAlias, locale);
87          if(target.format == null)
88          {
89              throw new Exception("Pattern alias '"+patternAlias+
90                  "' not defined in date formatter");
91          }
92          return target;
93      }
94  
95      /***
96       * Sets the formatting pattern.
97       *
98       * <p>The syntax of patterns is descirbed in
99       * <code>java.text.SimpleDateFormat</code> documentation.</p>
100      *
101      * @param pattern the formatting pattern.
102      * @return new DateFormatTool instance.
103      */
104     public DateFormatTool pattern(String pattern)
105     {
106         DateFormatTool target = createInstance(this);
107         target.format = new SimpleDateFormat(pattern, locale);
108         return target;
109     }
110 	
111     /***
112      * Returns the current date and time.
113      *
114      * @return the current date and time.
115      */
116     public Date now()
117     {
118         return new Date();
119     }
120 
121     /***
122      * Returns a Date object for a specified UNIX time.
123      *
124      * @param time the UNIX time.
125      * @return a Date object for the specified time.
126      */
127     public Date getDate(long time)
128     {
129         return new Date(time);
130     }
131 
132     /***
133      * Formats the Date object.
134      *
135      * @param date the Date.
136      * @return formatted date.
137      */
138     public String format(Date date)
139     {
140         if(date == null)
141         {
142             return null;
143         }
144         return format.format(date);
145     }
146 
147     /***
148      * Formats the date given as Unix time.
149      *
150      * @param date the date given as Unix time.
151      * @return formatted date.
152      */
153     public String format(long date)
154     {
155         return format(new Date(date));
156     }
157 
158     /***
159      * Returns fields of the specified date using local time zone.
160      *
161      * <p>The field order is as follows:
162      *  <ul>
163      *   <li>year</li>
164      *   <li>month (zero based)</li>
165      *   <li>day</li>
166      *   <li>hour of day (24h)</li>
167      *   <li>minute</li>
168      *   <li>second</li>
169      *   <li>millisecond</li>
170      *  </ul>
171      * </p>
172      * @param date the date to split.
173      * @return list of date fields, as described above.
174      */
175     public List getFields(Date date)
176     {
177         return getFields(date, TimeZone.getDefault());
178     }
179 
180     /***
181      * Returns fields of the specified date using specified time zone.
182      *
183      * <p>The field order is as follows:
184      *  <ul>
185      *   <li>year</li>
186      *   <li>month (zero based)</li>
187      *   <li>day</li>
188      *   <li>hour of day (24h)</li>
189      *   <li>minute</li>
190      *   <li>second</li>
191      *   <li>millisecond</li>
192      *  </ul>
193      * </p>
194      * @param date the date to split.
195      * @param zone the timezone to use.
196      * @return list of date fields, as described above.
197      */
198     public List getFields(Date date, TimeZone zone)
199     {
200         ArrayList<Integer> list = new ArrayList<Integer>(7);
201         if(date == null)
202         {
203             return list; 
204         }
205         Calendar cal = Calendar.getInstance(zone);
206         cal.setTime(date);
207         list.add(new Integer(cal.get(Calendar.YEAR)));
208         list.add(new Integer(cal.get(Calendar.MONTH)));
209         list.add(new Integer(cal.get(Calendar.DAY_OF_MONTH)));
210         list.add(new Integer(cal.get(Calendar.HOUR_OF_DAY)));
211         list.add(new Integer(cal.get(Calendar.MINUTE)));
212         list.add(new Integer(cal.get(Calendar.SECOND)));
213         list.add(new Integer(cal.get(Calendar.MILLISECOND)));
214         return list;
215     }
216 
217     /***
218      * Returns the time zone object with the specified id.
219      * 
220      * @param id time zone identifier string.
221      * @return a TimeZone object.
222      */
223     public TimeZone getTimeZone(String id)
224     {
225         return TimeZone.getTimeZone(id);
226     }
227 
228     /***
229      * Returns the available time zone ids.
230      * 
231      * @return list of recognized time zone ids.
232      */
233     public List getTimeZoneIds()
234     {
235         return Arrays.asList(TimeZone.getAvailableIDs());
236     }
237 
238 	
239     // implementation ------------------------------------------------------------------------------
240 
241     /***
242      * Creates the DateFormatTool instance for copying. This method is intended to be overriden by
243      * extending classes in order to provide DateFormatTool instances of proper class.
244      * 
245      * @param source copied object
246      * @return created instance of the linktool.
247      */
248     protected DateFormatTool createInstance(DateFormatTool source)
249     {
250         return new DateFormatTool(source.dateFormater, source.locale, source.format);
251     }
252 }