Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 662
نمايش فراداده

DateFormatjava.text

Java 1.1cloneable serializable

This class formats and parses dates and times in a locale-specific way. As an abstract class, it cannot be instantiated directly, but it provides a number of static methods that return instances of a concrete subclass you can use to format dates in a variety of ways. The getdateInstance( ) methods return a DateFormat object suitable for formatting dates in either the default locale or a specified locale. A formatting style may also optionally be specified; the constants FULL, LONG, MEDIUM, SHORT, and DEFAULT specify this style. Similarly, the getTimeInstance( ) methods return a DateFormat object that formats and parses times, and the geTDateTimeInstance( ) methods return a DateFormat object that formats both dates and times. These methods also optionally take a format style constant and a Locale. Finally, getInstance( ) returns a default DateFormat object that formats both dates and times in the SHORT format.

Once you have created a DateFormat object, you can use the setCalendar( ) and setTimeZone( ) methods if you want to format the date using a calendar or time zone other than the default. The various format( ) methods convert java.util.Date objects to strings using whatever format is encapsulated in the DateFormat object. The parse( ) and parseObject( ) methods perform the reverse operation; they parse a string formatted according to the rules of the DateFormat object and convert it into to a Date object. The DEFAULT, FULL, MEDIUM, LONG, and SHORT constants specify how verbose or compact the formatted date or time should be. The remaining constants, which all end with _FIELD, specify various fields of formatted dates and times and are used with the FieldPosition object that is optionally passed to format( ).

Figure 15-7. java.text.DateFormat

public abstract class

DateFormat extends Format { // Protected Constructors protected

DateFormat ( ); // Public Constants public static final int

AM_PM_FIELD ; =14 public static final int

DATE_FIELD ; =3 public static final int

DAY_OF_WEEK_FIELD ; =9 public static final int

DAY_OF_WEEK_IN_MONTH_FIELD ; =11 public static final int

DAY_OF_YEAR_FIELD ; =10 public static final int

DEFAULT ; =2 public static final int

ERA_FIELD ; =0 public static final int

FULL ; =0 public static final int

HOUR0_FIELD ; =16 public static final int

HOUR1_FIELD ; =15 public static final int

HOUR_OF_DAY0_FIELD ; =5 public static final int

HOUR_OF_DAY1_FIELD ; =4 public static final int

LONG ; =1 public static final int

MEDIUM ; =2 public static final int

MILLISECOND_FIELD ; =8 public static final int

MINUTE_FIELD ; =6 public static final int

MONTH_FIELD ; =2 public static final int

SECOND_FIELD ; =7 public static final int

SHORT ; =3 public static final int

TIMEZONE_FIELD ; =17 public static final int

WEEK_OF_MONTH_FIELD ; =13 public static final int

WEEK_OF_YEAR_FIELD ; =12 public static final int

YEAR_FIELD ; =1 // Nested Types

1.4 public static class

Field extends Format.Field; // Public Class Methods public static java.util.Locale[ ]

getAvailableLocales ( ); public static final DateFormat

getDateInstance ( ); public static final DateFormat

getDateInstance (int

style ); public static final DateFormat

getDateInstance (int

style , java.util.Locale

aLocale ); public static final DateFormat

getDateTimeInstance ( ); public static final DateFormat

getDateTimeInstance (int

dateStyle , int

timeStyle ); public static final DateFormat

getDateTimeInstance (int

dateStyle , int

timeStyle , java.util.Locale

aLocale ); public static final DateFormat

getInstance ( ); public static final DateFormat

getTimeInstance ( ); public static final DateFormat

getTimeInstance (int

style ); public static final DateFormat

getTimeInstance (int

style , java.util.Locale

aLocale ); // Public Instance Methods public final String

format (java.util.Date

date ); public abstract StringBuffer

format (java.util.Date

date , StringBuffer

toAppendTo , FieldPosition

fieldPosition ); public java.util.Calendar

getCalendar ( ); public NumberFormat

getNumberFormat ( ); public java.util.TimeZone

getTimeZone ( ); public boolean

isLenient ( ); public java.util.Date

parse (String

source ) throws ParseException; public abstract java.util.Date

parse (String

source , ParsePosition

pos ); public void

setCalendar (java.util.Calendar

newCalendar ); public void

setLenient (boolean

lenient ); public void

setNumberFormat (NumberFormat

newNumberFormat ); public void

setTimeZone (java.util.TimeZone

zone ); // Public Methods Overriding Format public Object

clone ( ); public final StringBuffer

format (Object

obj , StringBuffer

toAppendTo , FieldPosition

fieldPosition ); public Object

parseObject (String

source , ParsePosition

pos ); // Public Methods Overriding Object public boolean

equals (Object

obj ); public int

hashCode ( ); // Protected Instance Fields protected java.util.Calendar

calendar ; protected NumberFormat

numberFormat ; }

Subclasses

SimpleDateFormat