1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.james.util;
9
10 import java.text.DateFormat;
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.Locale;
15 import java.util.TimeZone;
16
17 /***
18 * A thread-safe date formatting class to produce dates formatted in accord with the
19 * specifications of RFC 977.
20 *
21 * @author Peter M. Goldstein <farsight@alum.mit.edu>
22 */
23 public class RFC977DateFormat implements SimplifiedDateFormat {
24
25 /***
26 * Internal date formatter for long date formats
27 */
28 private final SynchronizedDateFormat internalLongDateFormat;
29
30 /***
31 * Internal date formatter for short date formats
32 */
33 private final SynchronizedDateFormat internalShortDateFormat;
34
35 /***
36 * Constructor for RFC977DateFormat
37 */
38 public RFC977DateFormat() {
39 internalLongDateFormat = new SynchronizedDateFormat("yyyyMMdd HHmmss", Locale.ENGLISH);
40 internalShortDateFormat = new SynchronizedDateFormat("yyMMdd HHmmss", Locale.ENGLISH);
41 }
42
43 /***
44 * This method returns the long form of the RFC977 Date
45 *
46 * @return java.lang.String
47 * @param d Date
48 */
49 public String format(Date d) {
50 return internalLongDateFormat.format(d);
51 }
52
53 /***
54 * Parses text from the beginning of the given string to produce a date.
55 * The method may not use the entire text of the given string.
56 * <p>
57 * This method is designed to be thread safe, so we wrap our delegated
58 * parse method in an appropriate synchronized block.
59 *
60 * @param source A <code>String</code> whose beginning should be parsed.
61 * @return A <code>Date</code> parsed from the string.
62 * @throws ParseException if the beginning of the specified string
63 * cannot be parsed.
64 */
65 public Date parse(String source) throws ParseException {
66 source = source.trim();
67 if (source.indexOf(' ') == 6) {
68 return internalShortDateFormat.parse(source);
69 } else {
70 return internalLongDateFormat.parse(source);
71 }
72 }
73
74 /***
75 * Sets the time zone of this SynchronizedDateFormat object.
76 * @param zone the given new time zone.
77 */
78 public void setTimeZone(TimeZone zone) {
79 synchronized(this) {
80 internalShortDateFormat.setTimeZone(zone);
81 internalLongDateFormat.setTimeZone(zone);
82 }
83 }
84
85 /***
86 * Gets the time zone.
87 * @return the time zone associated with this SynchronizedDateFormat.
88 */
89 public TimeZone getTimeZone() {
90 synchronized(this) {
91 return internalShortDateFormat.getTimeZone();
92 }
93 }
94
95 /***
96 * Specify whether or not date/time parsing is to be lenient. With
97 * lenient parsing, the parser may use heuristics to interpret inputs that
98 * do not precisely match this object's format. With strict parsing,
99 * inputs must match this object's format.
100 * @param lenient when true, parsing is lenient
101 * @see java.util.Calendar#setLenient
102 */
103 public void setLenient(boolean lenient)
104 {
105 synchronized(this) {
106 internalShortDateFormat.setLenient(lenient);
107 internalLongDateFormat.setLenient(lenient);
108 }
109 }
110
111 /***
112 * Tell whether date/time parsing is to be lenient.
113 * @return whether this SynchronizedDateFormat is lenient.
114 */
115 public boolean isLenient()
116 {
117 synchronized(this) {
118 return internalShortDateFormat.isLenient();
119 }
120 }
121
122
123 /***
124 * Overrides equals
125 */
126 public boolean equals(Object obj) {
127 if (this == obj) {
128 return true;
129 }
130 if (!(obj instanceof RFC977DateFormat)) {
131 return false;
132 }
133 RFC977DateFormat theOtherRFC977DateFormat = (RFC977DateFormat)obj;
134 synchronized (this) {
135 return ((internalShortDateFormat.equals(theOtherRFC977DateFormat.internalShortDateFormat)) &&
136 (internalLongDateFormat.equals(theOtherRFC977DateFormat.internalLongDateFormat)));
137 }
138 }
139
140 /***
141 * Overrides hashCode
142 */
143 public int hashCode() {
144 return (int)(internalLongDateFormat.hashCode() & internalShortDateFormat.hashCode());
145 }
146
147 }
This page was automatically generated by Maven