1 | package net.sourceforge.calendardate; |
2 | import java.text.ParseException; |
3 | import java.text.SimpleDateFormat; |
4 | import java.util.Date; |
5 | import java.util.TimeZone; |
6 | |
7 | /** |
8 | * Formats and parses CalendarDates in a locale-sensitive manner based on a |
9 | * pattern string. <br> |
10 | * For example for a date representing June 12, 2004: |
11 | * <ul> |
12 | * <li>a pattern of "dd-MMM-yyyy" will result in "12-Jun-2004"; while |
13 | * <li>a pattern of "EEEEE, MMMM dd yyyy" will result in |
14 | * "Saturday, June 12 2004". |
15 | * </ul> |
16 | * <p> |
17 | * The pattern and format rules are the same as for java.text.SimpleDateFormat, |
18 | * except that only the following pattern letters are useful: <blockquote> |
19 | * <table border=1 cellspacing=0 cellpadding=2 summary="Chart shows pattern |
20 | * letters, date/time component, presentation, and examples."> |
21 | * <tr bgcolor="#ccccff"> |
22 | * <th align=left>Letter |
23 | * <th align=left>Date Component |
24 | * <th align=left>Examples |
25 | * <tr> |
26 | * <td><code>G</code> |
27 | * <td>Era designator |
28 | * <td><code>AD</code> |
29 | * <tr bgcolor="#eeeeff"> |
30 | * <td><code>y</code> |
31 | * <td>Year |
32 | * <td><code>1996</code>;<code>96</code> |
33 | * <tr> |
34 | * <td><code>M</code> |
35 | * <td>Month in year |
36 | * <td><code>July</code>;<code>Jul</code>;<code>07</code> |
37 | * <tr bgcolor="#eeeeff"> |
38 | * <td><code>d</code> |
39 | * <td>Day in month |
40 | * <td><code>10</code> |
41 | * <tr> |
42 | * <td><code>E</code> |
43 | * <td>Day in week |
44 | * <td><code>Tuesday</code>;<code>Tue</code> </table> </blockquote> |
45 | * |
46 | * @see java.text.SimpleDateFormat |
47 | */ |
48 | public class CalendarDateFormat { |
49 | |
50 | private SimpleDateFormat dateFormat; |
51 | |
52 | |
53 | /** |
54 | * Creates a format object based on the given date pattern. The resulting format |
55 | * is NOT lenient (i.e. "32-Jan-2004" will result in a ParseException, rather than |
56 | * returning 01-Jan-2004) |
57 | * @param pattern the pattern string - see class doc for more information |
58 | * @exception IllegalArgumentException if the given pattern is invalid |
59 | */ |
60 | public CalendarDateFormat(String pattern) { |
61 | dateFormat = new SimpleDateFormat(pattern); |
62 | dateFormat.setLenient(false); |
63 | dateFormat.setTimeZone(TimeZone.getDefault()); |
64 | } |
65 | |
66 | /** |
67 | * Creates a format object based on the given simple date format. |
68 | */ |
69 | public CalendarDateFormat(SimpleDateFormat format) { |
70 | dateFormat = (SimpleDateFormat) format.clone(); |
71 | dateFormat.setTimeZone(TimeZone.getDefault()); |
72 | } |
73 | |
74 | |
75 | |
76 | /** |
77 | * Creates a text-based representation of the given date |
78 | * @param date The date to be formatted |
79 | * @return The String form of the given date |
80 | */ |
81 | public String format(CalendarDate date) { |
82 | return dateFormat.format(date.toDate(dateFormat.getTimeZone())); |
83 | } |
84 | |
85 | /** |
86 | * Parses the given string to produce a CalendarDate. |
87 | * |
88 | * @param source The text to parse |
89 | * @return A CalendarDate matching the given string |
90 | * @throws java.text.ParseException If one of the following this true: |
91 | * <ol> |
92 | * <li>The String doesn't match the format of this pattern |
93 | * <li>The date represented by <code>source</code> is outside the |
94 | * valid CalendarDate range |
95 | * </ol> |
96 | */ |
97 | public CalendarDate parse(String source) throws ParseException { |
98 | TimeZone timeZone = dateFormat.getTimeZone(); |
99 | Date instant = dateFormat.parse(source); |
100 | if (CalendarDate.isOutsideRange(timeZone, instant)) { |
101 | throw new ParseException("Date is outside CalendarDate range", 0); |
102 | } |
103 | return new CalendarDate(timeZone, instant); |
104 | } |
105 | } |
106 | |