1 /*
2 * ==================================================================== The
3 * Apache Software License, Version 1.1
4 *
5 * Copyright (c) 2003 Digital Clash LLC. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The end-user documentation included with the redistribution, if any,
15 * must include the following acknowledgment: "This product includes software
16 * developed by the ChronicJ team (http://www.chronicj.org/)." Alternately,
17 * this acknowledgment may appear in the software itself, if and wherever such
18 * third-party acknowledgments normally appear.
19 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or
20 * promote products derived from this software without prior written
21 * permission. For written permission, please contact info@digitalclash.com.
22 * 5. Products derived from this software may not be called "ChronicJ",
23 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in their name,
24 * without prior written permission of Digital Clash LLC.
25 *
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * ==================================================================== This
37 * product includes software developed by the by the Apache Software Foundation
38 * (http://www.apache.org/).
39 * ====================================================================
40 */
41 package org.chronicj.impl;
42
43 import java.text.ParseException;
44 import java.text.SimpleDateFormat;
45
46 import java.util.Date;
47
48 /***
49 * Silly helper class for date formatting and other random utilities that don't
50 * squarely belong in other classes.
51 *
52 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a>
53 *
54 * @see java.text.SimpleDateFormat
55 */
56 public class DateUtil
57 {
58 private static final String DEFAULT_PATTERN =
59 "EEE, d MMM yyyy h:mm:ss.S a Z";
60 private static final SimpleDateFormat sdf =
61 new SimpleDateFormat(DEFAULT_PATTERN);
62
63 /***
64 * Date to String using the default pattern.
65 *
66 * @param aDate
67 * java.util.Date the date
68 *
69 * @return String formatted with pattern
70 *
71 * @see java.text.SimpleDateFormat
72 */
73 public static String format(Date aDate)
74 {
75 return format(aDate, DEFAULT_PATTERN);
76 }
77
78 /***
79 * Date to String using the supplied pattern.
80 *
81 * @param aDate
82 * java.util.Date the date
83 * @param pattern
84 * String the pattern
85 *
86 * @return String formatted with pattern
87 *
88 * @see java.text.SimpleDateFormat
89 */
90 public static String format(Date aDate, String pattern)
91 {
92 sdf.applyPattern(pattern);
93
94 return sdf.format(aDate);
95 }
96
97 /***
98 * Date format function using supplied pattern.
99 *
100 * @param dateString
101 * String the date
102 * @param pattern
103 * String the pattern
104 *
105 * @return java.util.Date
106 *
107 * @throws IllegalArgumentException
108 * if supplied dateString or pattern are invalid.
109 *
110 * @see java.text.SimpleDateFormat
111 */
112 public static Date parse(String dateString, String pattern)
113 {
114 try
115 {
116 sdf.applyPattern(pattern);
117
118 return sdf.parse(dateString);
119 } catch (ParseException e)
120 {
121 throw new IllegalArgumentException(e.getMessage());
122 }
123 }
124
125 /***
126 * Date format function using default pattern.
127 *
128 * @param dateString
129 * String the date
130 *
131 * @return java.util.Date
132 *
133 * @see java.text.SimpleDateFormat
134 */
135 public static Date parse(String dateString)
136 {
137 return parse(dateString, DEFAULT_PATTERN);
138 }
139 }
This page was automatically generated by Maven