1 |
/* DateFormat.java -- Class for formatting/parsing date/times |
/* DateFormat.java -- Class for formatting/parsing date/times |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
39 |
package java.text; |
package java.text; |
40 |
|
|
41 |
import java.util.*; |
import java.util.*; |
42 |
|
import java.io.InvalidObjectException; |
43 |
|
|
44 |
/** |
/** |
45 |
* @author Per Bothner <bothner@cygnus.com> |
* @author Per Bothner <bothner@cygnus.com> |
87 |
public static final int HOUR0_FIELD = 16; |
public static final int HOUR0_FIELD = 16; |
88 |
public static final int TIMEZONE_FIELD = 17; |
public static final int TIMEZONE_FIELD = 17; |
89 |
|
|
90 |
|
|
91 |
|
public static class Field extends Format.Field |
92 |
|
{ |
93 |
|
static final long serialVersionUID = 7441350119349544720L; |
94 |
|
|
95 |
|
private int calendarField; |
96 |
|
|
97 |
|
public static final DateFormat.Field ERA |
98 |
|
= new Field("era", Calendar.ERA); |
99 |
|
public static final DateFormat.Field YEAR |
100 |
|
= new Field("year", Calendar.YEAR); |
101 |
|
public static final DateFormat.Field MONTH |
102 |
|
= new Field("month", Calendar.MONTH); |
103 |
|
public static final DateFormat.Field DAY_OF_MONTH |
104 |
|
= new Field("day of month", Calendar.DAY_OF_MONTH); |
105 |
|
public static final DateFormat.Field HOUR_OF_DAY1 |
106 |
|
= new Field("hour of day 1", Calendar.HOUR_OF_DAY); |
107 |
|
public static final DateFormat.Field HOUR_OF_DAY0 |
108 |
|
= new Field("hour of day 0", Calendar.HOUR_OF_DAY); |
109 |
|
public static final DateFormat.Field MINUTE |
110 |
|
= new Field("minute", Calendar.MINUTE); |
111 |
|
public static final DateFormat.Field SECOND |
112 |
|
= new Field("second", Calendar.SECOND); |
113 |
|
public static final DateFormat.Field MILLISECOND |
114 |
|
= new Field("millisecond", Calendar.MILLISECOND); |
115 |
|
public static final DateFormat.Field DAY_OF_WEEK |
116 |
|
= new Field("day of week", Calendar.DAY_OF_WEEK); |
117 |
|
public static final DateFormat.Field DAY_OF_YEAR |
118 |
|
= new Field("day of year", Calendar.DAY_OF_YEAR); |
119 |
|
public static final DateFormat.Field DAY_OF_WEEK_IN_MONTH |
120 |
|
= new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH); |
121 |
|
public static final DateFormat.Field WEEK_OF_YEAR |
122 |
|
= new Field("week of year", Calendar.WEEK_OF_YEAR); |
123 |
|
public static final DateFormat.Field WEEK_OF_MONTH |
124 |
|
= new Field("week of month", Calendar.WEEK_OF_MONTH); |
125 |
|
public static final DateFormat.Field AM_PM |
126 |
|
= new Field("am/pm", Calendar.AM_PM); |
127 |
|
public static final DateFormat.Field HOUR1 |
128 |
|
= new Field("hour1", Calendar.HOUR); |
129 |
|
public static final DateFormat.Field HOUR0 |
130 |
|
= new Field("hour0", Calendar.HOUR); |
131 |
|
public static final DateFormat.Field TIME_ZONE |
132 |
|
= new Field("timezone", Calendar.ZONE_OFFSET); |
133 |
|
|
134 |
|
public static final DateFormat.Field[] allFields = |
135 |
|
{ |
136 |
|
ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY1, |
137 |
|
HOUR_OF_DAY0, MINUTE, SECOND, MILLISECOND, |
138 |
|
DAY_OF_WEEK, DAY_OF_YEAR, DAY_OF_WEEK_IN_MONTH, |
139 |
|
WEEK_OF_YEAR, WEEK_OF_MONTH, AM_PM, HOUR1, HOUR0, |
140 |
|
TIME_ZONE |
141 |
|
}; |
142 |
|
|
143 |
|
// For deserialization |
144 |
|
private Field() |
145 |
|
{ |
146 |
|
super(""); |
147 |
|
} |
148 |
|
|
149 |
|
protected Field(String name, int calendarField) |
150 |
|
{ |
151 |
|
super(name); |
152 |
|
this.calendarField = calendarField; |
153 |
|
} |
154 |
|
|
155 |
|
public int getCalendarField() |
156 |
|
{ |
157 |
|
return calendarField; |
158 |
|
} |
159 |
|
|
160 |
|
public static Field ofCalendarField(int calendarField) |
161 |
|
{ |
162 |
|
if (calendarField >= allFields.length || calendarField < 0) |
163 |
|
throw new IllegalArgumentException("no such calendar field (" |
164 |
|
+ calendarField + ")"); |
165 |
|
|
166 |
|
return allFields[calendarField]; |
167 |
|
} |
168 |
|
|
169 |
|
protected Object readResolve() throws InvalidObjectException |
170 |
|
{ |
171 |
|
String s = getName(); |
172 |
|
|
173 |
|
for (int i=0;i<allFields.length;i++) |
174 |
|
if (s.equals(allFields[i].getName())) |
175 |
|
return allFields[i]; |
176 |
|
|
177 |
|
throw new InvalidObjectException("no such DateFormat field called " + s); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
/** |
/** |
182 |
* This method initializes a new instance of <code>DateFormat</code>. |
* This method initializes a new instance of <code>DateFormat</code>. |
183 |
*/ |
*/ |
244 |
if (obj instanceof Number) |
if (obj instanceof Number) |
245 |
obj = new Date(((Number) obj).longValue()); |
obj = new Date(((Number) obj).longValue()); |
246 |
else if (! (obj instanceof Date)) |
else if (! (obj instanceof Date)) |
247 |
throw new IllegalArgumentException ("Cannot format given Object as a Date"); |
throw new IllegalArgumentException |
248 |
|
("Cannot format given Object as a Date"); |
249 |
|
|
250 |
return format ((Date) obj, buf, pos); |
return format ((Date) obj, buf, pos); |
251 |
} |
} |
252 |
|
|