java.text

Class SimpleDateFormat

Implemented Interfaces:
Cloneable, Serializable

public class SimpleDateFormat
extends DateFormat

SimpleDateFormat provides convenient methods for parsing and formatting dates using Gregorian calendars (see java.util.GregorianCalendar). This class is not thread-safe; external synchronisation should be applied if an instance is to be accessed from multiple threads.
See Also:
Serialized Form

Nested Class Summary

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

DateFormat.Field

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

Format.Field

Field Summary

Fields inherited from class java.text.DateFormat

AM_PM_FIELD, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR0_FIELD, HOUR1_FIELD, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD, calendar, numberFormat

Constructor Summary

SimpleDateFormat()
Constructs a SimpleDateFormat using the default pattern for the default locale.
SimpleDateFormat(String pattern)
Creates a date formatter using the specified non-localized pattern, with the default DateFormatSymbols for the default locale.
SimpleDateFormat(String pattern, DateFormatSymbols formatData)
Creates a date formatter using the specified non-localized pattern.
SimpleDateFormat(String pattern, Locale locale)
Creates a date formatter using the specified non-localized pattern, with the default DateFormatSymbols for the given locale.

Method Summary

void
applyLocalizedPattern(String pattern)
This method sets the formatting pattern that should be used by this object.
void
applyPattern(String pattern)
This method sets the formatting pattern that should be used by this object.
Object
clone()
Returns a copy of this instance of SimpleDateFormat.
boolean
equals(Object o)
This methods tests whether the specified object is equal to this object.
StringBuffer
format(Date date, StringBuffer buffer, FieldPosition pos)
AttributedCharacterIterator
formatToCharacterIterator(Object date)
Date
get2DigitYearStart()
Returns the start of the century used for two digit years.
DateFormatSymbols
getDateFormatSymbols()
This method returns a copy of the format symbol information used for parsing and formatting dates.
int
hashCode()
This method returns a hash value for this object.
Date
parse(String dateStr, ParsePosition pos)
This method parses the specified string into a date.
void
set2DigitYearStart(Date date)
Sets the start of the century used for two digit years.
void
setDateFormatSymbols(DateFormatSymbols formatData)
This method sets the format symbols information used for parsing and formatting dates.
String
toLocalizedPattern()
This method returns a string with the formatting pattern being used by this object.
String
toPattern()
This method returns a string with the formatting pattern being used by this object.
String
toString()
Returns a string representation of this class.

Methods inherited from class java.text.DateFormat

clone, equals, format, format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, hashCode, isLenient, parse, parse, parseObject, setCalendar, setLenient, setNumberFormat, setTimeZone

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

Constructor Details

SimpleDateFormat

public SimpleDateFormat()
Constructs a SimpleDateFormat using the default pattern for the default locale.

SimpleDateFormat

public SimpleDateFormat(String pattern)
Creates a date formatter using the specified non-localized pattern, with the default DateFormatSymbols for the default locale.
Parameters:
pattern - the pattern to use.
Throws:
NullPointerException - if the pattern is null.
IllegalArgumentException - if the pattern is invalid.

SimpleDateFormat

public SimpleDateFormat(String pattern,
                        DateFormatSymbols formatData)
Creates a date formatter using the specified non-localized pattern. The specified DateFormatSymbols will be used when formatting.
Parameters:
pattern - the non-localized pattern to use.
formatData - the formatting symbols to use.
Throws:
NullPointerException - if the pattern or formatData is null.
IllegalArgumentException - if the pattern is invalid.

SimpleDateFormat

public SimpleDateFormat(String pattern,
                        Locale locale)
Creates a date formatter using the specified non-localized pattern, with the default DateFormatSymbols for the given locale.
Parameters:
pattern - the non-localized pattern to use.
locale - the locale to use for the formatting symbols.
Throws:
NullPointerException - if the pattern is null.
IllegalArgumentException - if the pattern is invalid.

Method Details

applyLocalizedPattern

public void applyLocalizedPattern(String pattern)
This method sets the formatting pattern that should be used by this object. This string is localized.
Parameters:
pattern - The new format pattern.
Throws:
NullPointerException - if the pattern is null.
IllegalArgumentException - if the pattern is invalid.

applyPattern

public void applyPattern(String pattern)
This method sets the formatting pattern that should be used by this object. This string is not localized.
Parameters:
pattern - The new format pattern.
Throws:
NullPointerException - if the pattern is null.
IllegalArgumentException - if the pattern is invalid.

clone

public Object clone()
Returns a copy of this instance of SimpleDateFormat. The copy contains clones of the formatting symbols and the 2-digit year century start date.
Overrides:
clone in interface DateFormat

equals

public boolean equals(Object o)
This methods tests whether the specified object is equal to this object. This will be true if and only if the specified object:

  • Is not null.
  • Is an instance of SimpleDateFormat.
  • Is equal to this object at the superclass (i.e., DateFormat) level.
  • Has the same formatting pattern.
  • Is using the same formatting symbols.
  • Is using the same century for two digit years.
Overrides:
equals in interface DateFormat
Parameters:
o - The object to compare for equality against.
Returns:
true if the specified object is equal to this object, false otherwise.

format

public StringBuffer format(Date date,
                           StringBuffer buffer,
                           FieldPosition pos)
Overrides:
format in interface DateFormat

formatToCharacterIterator

public AttributedCharacterIterator formatToCharacterIterator(Object date)
            throws IllegalArgumentException
Overrides:
formatToCharacterIterator in interface Format

get2DigitYearStart

public Date get2DigitYearStart()
Returns the start of the century used for two digit years.
Returns:
A Date representing the start of the century for two digit years.

getDateFormatSymbols

public DateFormatSymbols getDateFormatSymbols()
This method returns a copy of the format symbol information used for parsing and formatting dates.
Returns:
a copy of the date format symbols.

hashCode

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

parse

public Date parse(String dateStr,
                  ParsePosition pos)
This method parses the specified string into a date.
Overrides:
parse in interface DateFormat
Parameters:
dateStr - The date string to parse.
pos - The input and output parse position
Returns:
The parsed date, or null if the string cannot be parsed.

set2DigitYearStart

public void set2DigitYearStart(Date date)
Sets the start of the century used for two digit years.
Parameters:
date - A Date representing the start of the century for two digit years.

setDateFormatSymbols

public void setDateFormatSymbols(DateFormatSymbols formatData)
This method sets the format symbols information used for parsing and formatting dates.
Parameters:
formatData - The date format symbols.
Throws:
NullPointerException - if formatData is null.

toLocalizedPattern

public String toLocalizedPattern()
This method returns a string with the formatting pattern being used by this object. This string is localized.
Returns:
The format string.

toPattern

public String toPattern()
This method returns a string with the formatting pattern being used by this object. This string is unlocalized.
Returns:
The format string.

toString

public String toString()
Returns a string representation of this class.
Overrides:
toString in interface Object
Returns:
a string representation of the SimpleDateFormat instance.

SimpleDateFormat.java -- A class for parsing/formating simple date constructs 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.