java.text

Class DateFormat

Implemented Interfaces:
Cloneable, Serializable
Known Direct Subclasses:
HTTPDateFormat, SimpleDateFormat

public abstract class DateFormat
extends Format
implements Cloneable

See Also:
Serialized Form

Nested Class Summary

static class
DateFormat.Field

Nested classes/interfaces inherited from class java.text.Format

Format.Field

Field Summary

static int
AM_PM_FIELD
Represents the position of the am/pm pattern character in the array of localized pattern characters.
static int
DATE_FIELD
Represents the position of the date or day of the month pattern character in the array of localized pattern characters.
static int
DAY_OF_WEEK_FIELD
Represents the position of the day of the week pattern character in the array of localized pattern characters.
static int
DAY_OF_WEEK_IN_MONTH_FIELD
Represents the position of the day of the week in the month pattern character in the array of localized pattern characters.
static int
DAY_OF_YEAR_FIELD
Represents the position of the day of the year pattern character in the array of localized pattern characters.
static int
DEFAULT
static int
ERA_FIELD
Represents the position of the era pattern character in the array of localized pattern characters.
static int
FULL
static int
HOUR0_FIELD
Represents the position of the 12 hour pattern character in the array of localized pattern characters.
static int
HOUR1_FIELD
Represents the position of the 12 hour pattern character in the array of localized pattern characters.
static int
HOUR_OF_DAY0_FIELD
Represents the position of the 24 hour pattern character in the array of localized pattern characters.
static int
HOUR_OF_DAY1_FIELD
Represents the position of the 24 hour pattern character in the array of localized pattern characters.
static int
LONG
static int
MEDIUM
static int
MILLISECOND_FIELD
Represents the position of the millisecond pattern character in the array of localized pattern characters.
static int
MINUTE_FIELD
Represents the position of the minute pattern character in the array of localized pattern characters.
static int
MONTH_FIELD
Represents the position of the month pattern character in the array of localized pattern characters.
static int
SECOND_FIELD
Represents the position of the second pattern character in the array of localized pattern characters.
static int
SHORT
static int
TIMEZONE_FIELD
Represents the position of the generic timezone pattern character in the array of localized pattern characters.
static int
WEEK_OF_MONTH_FIELD
Represents the position of the week of the month pattern character in the array of localized pattern characters.
static int
WEEK_OF_YEAR_FIELD
Represents the position of the week of the year pattern character in the array of localized pattern characters.
static int
YEAR_FIELD
Represents the position of the year pattern character in the array of localized pattern characters.
protected Calendar
calendar
protected NumberFormat
numberFormat

Constructor Summary

DateFormat()
This method initializes a new instance of DateFormat.

Method Summary

Object
clone()
This method returns a copy of this object.
boolean
equals(Object obj)
This method tests this object for equality against the specified object.
StringBuffer
format(Object obj, StringBuffer buf, FieldPosition pos)
This method formats the specified Object into a date string and appends it to the specified StringBuffer.
String
format(Date date)
Formats the date argument according to the pattern specified.
abstract StringBuffer
format(Date date, StringBuffer buf, FieldPosition pos)
This method formats a Date into a string and appends it to the specified StringBuffer.
static Locale[]
getAvailableLocales()
This method returns a list of available locales supported by this class.
Calendar
getCalendar()
This method returns the Calendar object being used by this object to parse/format datetimes.
static DateFormat
getDateInstance()
This method returns an instance of DateFormat that will format using the default formatting style for dates.
static DateFormat
getDateInstance(int style)
This method returns an instance of DateFormat that will format using the specified formatting style for dates.
static DateFormat
getDateInstance(int style, Locale loc)
This method returns an instance of DateFormat that will format using the specified formatting style for dates.
static DateFormat
getDateTimeInstance()
This method returns a new instance of DateFormat that formats both dates and times using the SHORT style.
static DateFormat
getDateTimeInstance(int dateStyle, int timeStyle)
This method returns a new instance of DateFormat that formats both dates and times using the DEFAULT style.
static DateFormat
getDateTimeInstance(int dateStyle, int timeStyle, Locale loc)
This method returns a new instance of DateFormat that formats both dates and times using the specified styles.
static DateFormat
getInstance()
This method returns a new instance of DateFormat that formats both dates and times using the SHORT style.
NumberFormat
getNumberFormat()
This method returns the NumberFormat object being used by this object to parse/format time values.
static DateFormat
getTimeInstance()
This method returns an instance of DateFormat that will format using the default formatting style for times.
static DateFormat
getTimeInstance(int style)
This method returns an instance of DateFormat that will format using the specified formatting style for times.
static DateFormat
getTimeInstance(int style, Locale loc)
This method returns an instance of DateFormat that will format using the specified formatting style for times.
TimeZone
getTimeZone()
This method returns the TimeZone object being used by this instance.
int
hashCode()
This method returns a hash value for this object.
boolean
isLenient()
This method indicates whether or not the parsing of date and time values should be done in a lenient value.
Date
parse(String source)
This method parses the specified date/time string.
abstract Date
parse(String source, ParsePosition pos)
This method parses the specified String into a Date.
Object
parseObject(String source, ParsePosition pos)
This method is identical to parse(String, ParsePosition), but returns its result as an Object instead of a Date.
void
setCalendar(Calendar calendar)
This method specified the Calendar that should be used by this object to parse/format datetimes.
void
setLenient(boolean lenient)
This method specifies whether or not this object should be lenient in the syntax it accepts while parsing date/time values.
void
setNumberFormat(NumberFormat numberFormat)
This method specifies the NumberFormat object that should be used by this object to parse/format times.
void
setTimeZone(TimeZone timeZone)
This method sets the time zone that should be used by this object.

Methods inherited from class java.text.Format

clone, format, format, formatToCharacterIterator, parseObject, parseObject

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

AM_PM_FIELD

public static final int AM_PM_FIELD
Represents the position of the am/pm pattern character in the array of localized pattern characters. In the U.S. locale, this is 'a'.
Field Value:
14

DATE_FIELD

public static final int DATE_FIELD
Represents the position of the date or day of the month pattern character in the array of localized pattern characters. In the U.S. locale, this is 'd'.
Field Value:
3

DAY_OF_WEEK_FIELD

public static final int DAY_OF_WEEK_FIELD
Represents the position of the day of the week pattern character in the array of localized pattern characters. In the U.S. locale, this is 'E'.
Field Value:
9

DAY_OF_WEEK_IN_MONTH_FIELD

public static final int DAY_OF_WEEK_IN_MONTH_FIELD
Represents the position of the day of the week in the month pattern character in the array of localized pattern characters. In the U.S. locale, this is 'F'.
Field Value:
11

DAY_OF_YEAR_FIELD

public static final int DAY_OF_YEAR_FIELD
Represents the position of the day of the year pattern character in the array of localized pattern characters. In the U.S. locale, this is 'D'.
Field Value:
10

DEFAULT

public static final int DEFAULT
Field Value:
2

ERA_FIELD

public static final int ERA_FIELD
Represents the position of the era pattern character in the array of localized pattern characters. For example, 'AD' is an era used in the Gregorian calendar system. In the U.S. locale, this is 'G'.
Field Value:
0

FULL

public static final int FULL
Field Value:
0

HOUR0_FIELD

public static final int HOUR0_FIELD
Represents the position of the 12 hour pattern character in the array of localized pattern characters. In the U.S. locale, this is 'K'. This field numbers hours from 0 to 11.
Field Value:
16

HOUR1_FIELD

public static final int HOUR1_FIELD
Represents the position of the 12 hour pattern character in the array of localized pattern characters. In the U.S. locale, this is 'h'. This field numbers hours from 1 to 12.
Field Value:
15

HOUR_OF_DAY0_FIELD

public static final int HOUR_OF_DAY0_FIELD
Represents the position of the 24 hour pattern character in the array of localized pattern characters. In the U.S. locale, this is 'H'. This field numbers hours from 0 to 23.
Field Value:
5

HOUR_OF_DAY1_FIELD

public static final int HOUR_OF_DAY1_FIELD
Represents the position of the 24 hour pattern character in the array of localized pattern characters. In the U.S. locale, this is 'k'. This field numbers hours from 1 to 24.
Field Value:
4

LONG

public static final int LONG
Field Value:
1

MEDIUM

public static final int MEDIUM
Field Value:
2

MILLISECOND_FIELD

public static final int MILLISECOND_FIELD
Represents the position of the millisecond pattern character in the array of localized pattern characters. In the U.S. locale, this is 'S'.
Field Value:
8

MINUTE_FIELD

public static final int MINUTE_FIELD
Represents the position of the minute pattern character in the array of localized pattern characters. In the U.S. locale, this is 'm'.
Field Value:
6

MONTH_FIELD

public static final int MONTH_FIELD
Represents the position of the month pattern character in the array of localized pattern characters. In the U.S. locale, this is 'M'.
Field Value:
2

SECOND_FIELD

public static final int SECOND_FIELD
Represents the position of the second pattern character in the array of localized pattern characters. In the U.S. locale, this is 's'.
Field Value:
7

SHORT

public static final int SHORT
Field Value:
3

TIMEZONE_FIELD

public static final int TIMEZONE_FIELD
Represents the position of the generic timezone pattern character in the array of localized pattern characters. In the U.S. locale, this is 'z'.
Field Value:
17

WEEK_OF_MONTH_FIELD

public static final int WEEK_OF_MONTH_FIELD
Represents the position of the week of the month pattern character in the array of localized pattern characters. In the U.S. locale, this is 'W'.
Field Value:
13

WEEK_OF_YEAR_FIELD

public static final int WEEK_OF_YEAR_FIELD
Represents the position of the week of the year pattern character in the array of localized pattern characters. In the U.S. locale, this is 'w'.
Field Value:
12

YEAR_FIELD

public static final int YEAR_FIELD
Represents the position of the year pattern character in the array of localized pattern characters. In the U.S. locale, this is 'y'.
Field Value:
1

calendar

protected Calendar calendar

numberFormat

protected NumberFormat numberFormat

Constructor Details

DateFormat

protected DateFormat()
This method initializes a new instance of DateFormat.

Method Details

clone

public Object clone()
This method returns a copy of this object.
Overrides:
clone in interface Format
Returns:
A copy of this object.

equals

public boolean equals(Object obj)
This method tests this object for equality against the specified object. The two objects will be considered equal if an only if the specified object:

  • Is not null.
  • Is an instance of DateFormat.
  • Has equal numberFormat field as this object.
  • Has equal (Calendar) TimeZone rules as this object.
  • Has equal (Calendar) isLenient results.
  • Has equal Calendar first day of week and minimal days in week values.
Note that not all properties of the Calendar are relevant for a DateFormat. For formatting only the fact whether or not the TimeZone has the same rules and whether the calendar is lenient and has the same week rules is compared for this implementation of equals. Other properties of the Calendar (such as the time) are not taken into account.
Overrides:
equals in interface Object
Parameters:
obj - The object to test for equality against.
Returns:
true if the specified object is equal to this object, false otherwise.

format

public final StringBuffer format(Object obj,
                                 StringBuffer buf,
                                 FieldPosition pos)
This method formats the specified Object into a date string and appends it to the specified StringBuffer. The specified object must be an instance of Number or Date or an IllegalArgumentException will be thrown.
Overrides:
format in interface Format
Parameters:
obj - The Object to format.
buf - The StringBuffer to append the resultant String to.
pos - Is updated to the start and end index of the specified field.
Returns:
The StringBuffer supplied on input, with the formatted date/time appended.

format

public final String format(Date date)
Formats the date argument according to the pattern specified.
Parameters:
date - The formatted date.

format

public abstract StringBuffer format(Date date,
                                    StringBuffer buf,
                                    FieldPosition pos)
This method formats a Date into a string and appends it to the specified StringBuffer.
Parameters:
date - The Date value to format.
buf - The StringBuffer to append the resultant String to.
pos - Is updated to the start and end index of the specified field.
Returns:
The StringBuffer supplied on input, with the formatted date/time appended.

getAvailableLocales

public static Locale[] getAvailableLocales()
This method returns a list of available locales supported by this class.

getCalendar

public Calendar getCalendar()
This method returns the Calendar object being used by this object to parse/format datetimes.
Returns:
The Calendar being used by this object.
See Also:
Calendar

getDateInstance

public static final DateFormat getDateInstance()
This method returns an instance of DateFormat that will format using the default formatting style for dates.
Returns:
A new DateFormat instance.

getDateInstance

public static final DateFormat getDateInstance(int style)
This method returns an instance of DateFormat that will format using the specified formatting style for dates.
Parameters:
style - The type of formatting to perform.
Returns:
A new DateFormat instance.

getDateInstance

public static final DateFormat getDateInstance(int style,
                                               Locale loc)
This method returns an instance of DateFormat that will format using the specified formatting style for dates. The specified localed will be used in place of the default.
Parameters:
style - The type of formatting to perform.
loc - The desired locale.
Returns:
A new DateFormat instance.

getDateTimeInstance

public static final DateFormat getDateTimeInstance()
This method returns a new instance of DateFormat that formats both dates and times using the SHORT style.
Returns:
A new DateFormatinstance.

getDateTimeInstance

public static final DateFormat getDateTimeInstance(int dateStyle,
                                                   int timeStyle)
This method returns a new instance of DateFormat that formats both dates and times using the DEFAULT style.
Returns:
A new DateFormatinstance.

getDateTimeInstance

public static final DateFormat getDateTimeInstance(int dateStyle,
                                                   int timeStyle,
                                                   Locale loc)
This method returns a new instance of DateFormat that formats both dates and times using the specified styles.
Parameters:
dateStyle - The desired style for date formatting.
timeStyle - The desired style for time formatting
Returns:
A new DateFormatinstance.

getInstance

public static final DateFormat getInstance()
This method returns a new instance of DateFormat that formats both dates and times using the SHORT style.
Returns:
A new DateFormatinstance.

getNumberFormat

public NumberFormat getNumberFormat()
This method returns the NumberFormat object being used by this object to parse/format time values.
Returns:
The NumberFormat in use by this object.

getTimeInstance

public static final DateFormat getTimeInstance()
This method returns an instance of DateFormat that will format using the default formatting style for times.
Returns:
A new DateFormat instance.

getTimeInstance

public static final DateFormat getTimeInstance(int style)
This method returns an instance of DateFormat that will format using the specified formatting style for times.
Parameters:
style - The type of formatting to perform.
Returns:
A new DateFormat instance.

getTimeInstance

public static final DateFormat getTimeInstance(int style,
                                               Locale loc)
This method returns an instance of DateFormat that will format using the specified formatting style for times. The specified localed will be used in place of the default.
Parameters:
style - The type of formatting to perform.
loc - The desired locale.
Returns:
A new DateFormat instance.

getTimeZone

public TimeZone getTimeZone()
This method returns the TimeZone object being used by this instance.
Returns:
The time zone in use.

hashCode

public int hashCode()
This method returns a hash value for this object.
Overrides:
hashCode in interface Object
Returns:
A hash value for this object.

isLenient

public boolean isLenient()
This method indicates whether or not the parsing of date and time values should be done in a lenient value.
Returns:
true if date/time parsing is lenient, false otherwise.

parse

public Date parse(String source)
            throws ParseException
This method parses the specified date/time string.
Parameters:
source - The string to parse.
Returns:
The resultant date.
Throws:
ParseException - If the specified string cannot be parsed.

parse

public abstract Date parse(String source,
                           ParsePosition pos)
This method parses the specified String into a Date. The pos argument contains the starting parse position on method entry and the ending parse position on method exit.
Parameters:
source - The string to parse.
pos - The starting parse position in entry, the ending parse position on exit.
Returns:
The parsed date, or null if the string cannot be parsed.

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
This method is identical to parse(String, ParsePosition), but returns its result as an Object instead of a Date.
Overrides:
parseObject in interface Format
Parameters:
source - The string to parse.
pos - The starting parse position in entry, the ending parse position on exit.
Returns:
The parsed date, or null if the string cannot be parsed.

setCalendar

public void setCalendar(Calendar calendar)
This method specified the Calendar that should be used by this object to parse/format datetimes.
Parameters:
calendar - The new Calendar for this object.
See Also:
Calendar

setLenient

public void setLenient(boolean lenient)
This method specifies whether or not this object should be lenient in the syntax it accepts while parsing date/time values.
Parameters:
lenient - true if parsing should be lenient, false otherwise.

setNumberFormat

public void setNumberFormat(NumberFormat numberFormat)
This method specifies the NumberFormat object that should be used by this object to parse/format times.
Parameters:
numberFormat - The NumberFormat in use by this object.

setTimeZone

public void setTimeZone(TimeZone timeZone)
This method sets the time zone that should be used by this object.
Parameters:
timeZone - The new time zone.

DateFormat.java -- Class for formatting/parsing date/times Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.