KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > SimpleISO8601DateFormat


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17
18 import java.io.*;
19 import java.text.*;
20 import java.util.*;
21
22 /**
23  * <code>SimpleISO8601DateFormat</code> is a concrete class for formatting and
24  * parsing ISO 8601 format dates. It allows for formatting
25  * (date -> text), parsing (text -> date), and normalization.
26  *
27  * @see java.util.Calendar
28  * @see java.util.GregorianCalendar
29  * @see java.util.TimeZone
30  * @see DateFormat
31  * @see DateFormatSymbols
32  * @see DecimalFormat
33  * @version 1.31 04/22/98
34  * @author Mark Davis, Chen-Lieh Huang, Alan Liu
35  */

36 public class SimpleISO8601DateFormat extends DateFormat {
37
38     // the official serial version ID which says cryptically
39
// which version we're compatible with
40
static final long serialVersionUID = 4774881970558875024L;
41
42     // the internal serial version which says which version was written
43
// - 0 (default) for version up to JDK 1.1.3
44
// - 1 for version from JDK 1.1.4, which includes a new field
45
static final int currentSerialVersion = 1;
46     private int serialVersionOnStream = currentSerialVersion;
47 /**
48  * Construct a SimpleDateFormat using the default pattern for the default
49  * locale. <b>Note:</b> Not all locales support SimpleDateFormat; for full
50  * generality, use the factory methods in the DateFormat class.
51  *
52  * @see java.text.DateFormat
53  */

54 public SimpleISO8601DateFormat() {
55     initialize();
56 }
57 /**
58  * Overrides Cloneable
59  */

60 public Object JavaDoc clone() {
61     SimpleISO8601DateFormat other = (SimpleISO8601DateFormat) super.clone();
62 // other.formatData = (DateFormatSymbols) formatData.clone();
63
return other;
64 }
65 /**
66  * Override equals.
67  */

68 public boolean equals(Object JavaDoc obj) {
69     if (!super.equals(obj))
70         return false; // super does class check
71

72     // todo: I think we are supposed to check if they are equivalent, but for now a class check will do. In fact I think
73
// just being the same class is adequate.
74

75     return true;
76 }
77 /**
78  * Overrides DateFormat
79  * <p>Formats a date or time, which is the standard millis
80  * since January 1, 1970, 00:00:00 GMT.
81  * @param date the date-time value to be formatted into a date-time string.
82  * @param toAppendTo where the new date-time text is to be appended.
83  * @param pos the formatting position. On input: an alignment field,
84  * if desired. On output: the offsets of the alignment field.
85  * @return the formatted date-time string.
86  * @see java.util.DateFormat
87  */

88 public StringBuffer JavaDoc format(Date date, StringBuffer JavaDoc toAppendTo, FieldPosition pos) {
89     java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss");
90     df.setTimeZone(TimeZone.getTimeZone("GMT"));
91     df.format(date, toAppendTo, pos);
92     Calendar calendar = Calendar.getInstance();
93     calendar.setTime(date);
94     int dstoff = calendar.get(Calendar.DST_OFFSET);
95     int tzoff = calendar.get(Calendar.ZONE_OFFSET);
96     
97     tzoff += dstoff;
98     tzoff /= (1000 * 60);
99     if (tzoff != 0) {
100         if (tzoff < 0) {
101             toAppendTo.append("-");
102             tzoff *= -1;
103         } else {
104             toAppendTo.append("+");
105         }
106         int hr = tzoff / 60;
107         if (hr < 10)
108             toAppendTo.append("0");
109         toAppendTo.append("" + hr + ":");
110         int mn = tzoff % 60;
111         if (mn < 10)
112             toAppendTo.append("0");
113         toAppendTo.append(mn);
114     } else {
115         toAppendTo.append("Z");
116     }
117     //System.out.println( toAppendTo );
118
/*
119        Calendar calendar = getCalendar();
120        // Initialize
121        pos.beginIndex = pos.endIndex = 0;
122        int posfield = pos.getField();
123     
124     // Convert input date to time field list
125     calendar.setTime(date);
126     
127     
128     int tval = calendar.get(Calendar.YEAR );
129     if (posfield==DateFormat.YEAR_FIELD) {
130        pos.beginIndex = toAppendTo.length();
131        pos.endIndex = pos.beginIndex+4;
132     }
133     toAppendTo.append( tval );
134     toAppendTo.append( '-' );
135     if (posfield==DateFormat.MONTH_FIELD) {
136        pos.beginIndex = toAppendTo.length();
137        pos.endIndex = pos.beginIndex+2;
138     }
139     tval = calendar.get(Calendar.MONTH ) + 1 ; // apparently we need to increment these
140     if (tval<10) toAppendTo.append( '0' );
141     toAppendTo.append( tval );
142     
143     toAppendTo.append( '-' );
144     if (posfield==DateFormat.DATE_FIELD) {
145        pos.beginIndex = toAppendTo.length();
146        pos.endIndex = pos.beginIndex+2;
147     }
148     tval = calendar.get(Calendar.DAY_OF_MONTH );
149     if (tval<10) toAppendTo.append( '0' );
150     toAppendTo.append( tval );
151     
152     toAppendTo.append( 'T' );
153     if (posfield==DateFormat.HOUR_OF_DAY0_FIELD) {
154        pos.beginIndex = toAppendTo.length();
155        pos.endIndex = pos.beginIndex+2;
156     }
157     tval = calendar.get(Calendar.HOUR );
158     if (tval<10) toAppendTo.append( '0' );
159     toAppendTo.append( tval );
160     
161     toAppendTo.append( ':' );
162     if (posfield==DateFormat.MINUTE_FIELD) {
163        pos.beginIndex = toAppendTo.length();
164        pos.endIndex = pos.beginIndex+2;
165     }
166     tval = calendar.get(Calendar.MINUTE );
167     if (tval<10) toAppendTo.append( '0' );
168     toAppendTo.append( tval );
169     
170     toAppendTo.append( ':' );
171     if (posfield==DateFormat.SECOND_FIELD) {
172        pos.beginIndex = toAppendTo.length();
173        pos.endIndex = pos.beginIndex+2;
174     }
175     tval = calendar.get(Calendar.SECOND );
176     if (tval<10) toAppendTo.append( '0' );
177     toAppendTo.append( tval );
178     
179     toAppendTo.append( 'Z' );
180       */

181     return toAppendTo;
182 }
183 /* Initialize calendar and numberFormat fields */
184
185 private void initialize() {
186     // The format object must be constructed using the symbols for this zone.
187
// However, the calendar should use the current default TimeZone.
188
// If this is not contained in the locale zone strings, then the zone
189
// will be formatted using generic GMT+/-H:MM nomenclature.
190
TimeZone tz = TimeZone.getTimeZone("UDT");
191     calendar = Calendar.getInstance(tz);
192     // numberFormat isn't used by us, but we inherit behavior that we have to respect.
193
NumberFormat numberFormat = NumberFormat.getInstance();
194     /*
195     numberFormat.setGroupingUsed(false);
196     if (numberFormat instanceof DecimalFormat)
197     ((DecimalFormat)numberFormat).setDecimalSeparatorAlwaysShown(false);
198     numberFormat.setParseIntegerOnly(true); // So that dd.mm.yy can be parsed
199     numberFormat.setMinimumFractionDigits(0); // To prevent "Jan 1.00, 1997.00"
200     */

201     setNumberFormat(numberFormat);
202     /*
203     initializeDefaultCentury();
204     */

205 }
206 /**
207  * Overrides DateFormat
208  * @see java.util.DateFormat
209  */

210 public Date parse(String JavaDoc text, java.text.ParsePosition JavaDoc pos) {
211     int start = pos.getIndex();
212     Calendar calendar = getCalendar();
213     calendar.clear(); // Clears all the time fields
214

215     int year = Integer.parseInt(text.substring(start, start + 4));
216     int month = Integer.parseInt(text.substring(start + 5, start + 7));
217     int day = Integer.parseInt(text.substring(start + 8, start + 10));
218     int hour = Integer.parseInt(text.substring(start + 11, start + 13));
219     int minute = Integer.parseInt(text.substring(start + 14, start + 16));
220     int second = Integer.parseInt(text.substring(start + 17, start + 19));
221     if (text.substring(start + 19, start + 20).equals("Z")) {
222         // GMT
223
pos.setIndex(start + 20);
224     } else {
225         int offhour = Integer.parseInt(text.substring(start + 20, start + 22));
226         int offmin = Integer.parseInt(text.substring(start + 23, start + 25));
227         int sum = (offhour * 60 + offmin) * 60 * 1000; // in millis
228
if (text.substring(start + 19, start + 20).equals("-"))
229             sum *= -1; // NYC is "-04:00"
230
calendar.set(Calendar.ZONE_OFFSET, sum); // NYC is ZONE_OFFSET=5 or 4
231
//calendar.set( Calendar.DST_OFFSET, sum );
232
pos.setIndex(start + 25);
233     }
234
235     // todo: return position.
236

237     calendar.set(year, month - 1, day, hour, minute, second);
238     Date retval = calendar.getTime();
239     //System.out.println( " "+ text + "******" + retval );
240
return retval;
241 }
242 /**
243  * Override readObject.
244  */

245 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException JavaDoc {
246     stream.defaultReadObject();
247     /*
248      if (serialVersionOnStream < 1) {
249      // didn't have defaultCenturyStart field
250      initializeDefaultCentury();
251      }
252      else {
253      // fill in dependent transient field
254      parseAmbiguousDatesAsAfter(defaultCenturyStart);
255      }
256     */

257     serialVersionOnStream = currentSerialVersion;
258 }
259 }
260
Popular Tags