KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > DateUtilities


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8
9  1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions, and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions, and the disclaimer that follows
14     these conditions in the documentation and/or other materials
15     provided with the distribution.
16  3. The name "JPublish" must not be used to endorse or promote products
17     derived from this software without prior written permission. For
18     written permission, please contact info@aetrion.com.
19
20  4. Products derived from this software may not be called "JPublish", nor
21     may "JPublish" appear in their name, without prior written permission
22     from Aetrion LLC (info@aetrion.com).
23
24  In addition, the authors of this software request (but do not require)
25  that you include in the end-user documentation provided with the
26  redistribution and/or in the software itself an acknowledgement equivalent
27  to the following:
28      "This product includes software developed by
29       Aetrion LLC (http://www.aetrion.com/)."
30  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
31  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
34  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  POSSIBILITY OF SUCH DAMAGE.
41  For more information on JPublish, please see <http://www.jpublish.org/>.
42
43  */

44 package org.jpublish.util;
45
46 import java.text.ParseException JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48 import java.util.Date JavaDoc;
49
50 /**
51  * Utility class for parsing and formatting dates. An instance of this class can be dropped in the context to allow
52  * parsing and formatting dates from within view templates.
53  *
54  * @author Anthony Eden
55  * @since 1.4
56  */

57 public class DateUtilities {
58
59     private final static DateUtilities dateUtilities = new DateUtilities();
60
61     /**
62      * Get a shared instance of the DateUtilities class.
63      *
64      * @return A DateUtilities instance
65      */

66     public static DateUtilities getInstance() {
67         return dateUtilities;
68     }
69
70     /**
71      * Format the specified date using the specified format String. The format String follows the rules specified in the
72      * <code>java.text.SimpleDateFormat</code> class.
73      *
74      * @param date The date
75      * @param format The format String
76      * @return A formatted Date String
77      */

78
79     public String JavaDoc format(Date JavaDoc date, String JavaDoc format) {
80         if (date == null) {
81             return "";
82         }
83         SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc(format);
84         return fmt.format(date);
85     }
86
87     /**
88      * Parse the specified date String using the specified format String. The format String follows the rules specified
89      * in the <code>java.text.SimpleDateFormat</code> class.
90      *
91      * @param date The date String
92      * @param format The format String
93      * @return A Date object
94      * @throws ParseException
95      */

96
97     public Date JavaDoc parse(String JavaDoc date, String JavaDoc format) throws ParseException JavaDoc {
98         SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc(format);
99         return fmt.parse(date);
100     }
101
102 }
103
104
Popular Tags