1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* Copyright (C) 1998, 1999 Cygnus Solutions
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
package java.text;
import java.util.*;
/**
* @author Per Bothner <bothner@cygnus.com>
* @date October 25, 1998.
*/
/* Written using "Java Class Libraries", 2nd edition, plus online
* API docs for JDK 1.2 beta from http://www.javasoft.com.
* Status: Mostly complete; search for FIXME to see omissions.
*/
public abstract class DateFormat extends Format implements Cloneable
{
protected Calendar calendar;
protected NumberFormat numberFormat;
// (Values determined using a test program.)
public final static int FULL = 0;
public final static int LONG = 1;
public final static int MEDIUM = 2;
public final static int SHORT = 3;
public final static int DEFAULT = MEDIUM;
public final static int ERA_FIELD = 0;
public final static int YEAR_FIELD = 1;
public final static int MONTH_FIELD = 2;
public final static int DATE_FIELD = 3;
public final static int HOUR_OF_DAY1_FIELD = 4;
public final static int HOUR_OF_DAY0_FIELD = 5;
public final static int MINUTE_FIELD = 6;
public final static int SECOND_FIELD = 7;
public final static int MILLISECOND_FIELD = 8;
public final static int DAY_OF_WEEK_FIELD = 9;
public final static int DAY_OF_YEAR_FIELD = 10;
public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
public final static int WEEK_OF_YEAR_FIELD = 12;
public final static int WEEK_OF_MONTH_FIELD = 13;
public final static int AM_PM_FIELD = 14;
public final static int HOUR1_FIELD = 15;
public final static int HOUR0_FIELD = 16;
public final static int TIMEZONE_FIELD = 17;
protected DateFormat ()
{
}
public boolean equals (Object obj)
{
if (! (obj instanceof DateFormat))
return false;
DateFormat d = (DateFormat) obj;
return calendar.equals(d.calendar) && numberFormat.equals(d.numberFormat);
}
public final StringBuffer format (Object obj,
StringBuffer buf, FieldPosition pos)
{
if (obj instanceof Number)
return format (new Date(((Number) obj).longValue()), buf, pos);
return format ((Date) obj, buf, pos);
}
public final String format (Date date)
{
StringBuffer sb = new StringBuffer ();
format (date, sb, new FieldPosition (MONTH_FIELD));
return sb.toString();
}
public abstract StringBuffer format (Date date,
StringBuffer buf, FieldPosition pos);
public static Locale[] getAvailableLocales ()
{
return null; // FIXME
}
public Calendar getCalendar ()
{
return calendar;
}
private static final DateFormat computeInstance (int style, Locale loc,
boolean use_date,
boolean use_time)
{
return computeInstance (style, style, loc, use_date, use_time);
}
private static final DateFormat computeInstance (int dateStyle,
int timeStyle,
Locale loc,
boolean use_date,
boolean use_time)
{
ResourceBundle res;
try
{
res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
}
catch (MissingResourceException x)
{
res = null;
}
String pattern = null;
if (use_date)
{
String name, def;
switch (dateStyle)
{
case FULL:
name = "fullDateFormat";
def = "EEEE MMMM d, yyyy G";
break;
case LONG:
name = "longDateFormat";
def = "MMMM d, yyyy";
break;
case MEDIUM:
name = "mediumDateFormat";
def = "d-MMM-yy";
break;
case SHORT:
name = "shortDateFormat";
def = "M/d/yy";
break;
default:
throw new IllegalArgumentException ();
}
try
{
pattern = res == null ? def : res.getString(name);
}
catch (MissingResourceException x)
{
pattern = def;
}
}
if (use_time)
{
if (pattern == null)
pattern = "";
else
pattern += " ";
String name, def;
switch (timeStyle)
{
case FULL:
name = "fullTimeFormat";
def = "h:mm:ss;S 'o''clock' a z";
break;
case LONG:
name = "longTimeFormat";
def = "h:mm:ss a z";
break;
case MEDIUM:
name = "mediumTimeFormat";
def = "h:mm:ss a";
break;
case SHORT:
name = "shortTimeFormat";
def = "h:mm a";
break;
default:
throw new IllegalArgumentException ();
}
String s;
try
{
s = res == null ? def : res.getString(name);
}
catch (MissingResourceException x)
{
s = def;
}
pattern += s;
}
return new SimpleDateFormat (pattern, loc);
}
public static final DateFormat getDateInstance ()
{
return getDateInstance (DEFAULT, Locale.getDefault());
}
public static final DateFormat getDateInstance (int style)
{
return getDateInstance (style, Locale.getDefault());
}
public static final DateFormat getDateInstance (int style, Locale loc)
{
return computeInstance (style, loc, true, false);
}
public static final DateFormat getDateTimeInstance ()
{
return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
}
public static final DateFormat getDateTimeInstance (int style)
{
return getDateTimeInstance (style, style, Locale.getDefault());
}
public static final DateFormat getDateTimeInstance (int dateStyle,
int timeStyle)
{
return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
}
public static final DateFormat getDateTimeInstance (int dateStyle,
int timeStyle,
Locale loc)
{
return computeInstance (dateStyle, timeStyle, loc, true, true);
}
public static final DateFormat getInstance ()
{
// JCL book says SHORT.
return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
}
public NumberFormat getNumberFormat ()
{
return numberFormat;
}
public static final DateFormat getTimeInstance ()
{
return getTimeInstance (DEFAULT, Locale.getDefault());
}
public static final DateFormat getTimeInstance (int style)
{
return getTimeInstance (style, Locale.getDefault());
}
public static final DateFormat getTimeInstance (int style, Locale loc)
{
return computeInstance (style, loc, false, true);
}
public TimeZone getTimeZone ()
{
return calendar.getTimeZone();
}
public int hashCode ()
{
int hash = calendar.hashCode();
if (numberFormat != null)
hash ^= numberFormat.hashCode();
return hash;
}
public boolean isLenient ()
{
return calendar.isLenient();
}
public Date parse (String source) throws ParseException
{
ParsePosition pos = new ParsePosition(0);
Date result = parse (source, pos);
if (result == null)
{
int index = pos.getErrorIndex();
if (index < 0)
index = pos.getIndex();
throw new ParseException("invalid Date syntax", index);
}
return result;
}
public abstract Date parse (String source, ParsePosition pos);
public Object parseObject (String source, ParsePosition pos)
{
return parse(source, pos);
}
public void setCalendar (Calendar calendar)
{
this.calendar = calendar;
}
public void setLenient (boolean lenient)
{
calendar.setLenient(lenient);
}
public void setNumberFormat (NumberFormat numberFormat)
{
this.numberFormat = numberFormat;
}
public void setTimeZone (TimeZone timeZone)
{
calendar.setTimeZone(timeZone);
}
}
|