Abstract Class DateFormat in Java
DateFormat 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 that we can use to format dates in a variety of ways.
The DateFormat class formats and parses dates and times in a locale-specific manner. DateFormat is an abstract class, but it provides factory methods that return useful instances of DateFormat subclasses. These factory methods come in three groups:
- The getDateInstance() methods return objects that format and parse only dates.
- The getDateTimeInstance() methods return objects that format and parse date and time combinations.
- The getTimeInstance() methods return objects that format only times.
Certain of these factory methods allow you to specify the style, or length, of the resulting data and time strings. The interpretation of the style parameter is locale-specific. For the locale Locale.US, the styles and their results are as follows:
FULL – Tuesday, March 04, 2019, 12:00:00 o’clock AM EST
LONG – March 04, 2019 12:00:00 AM EST
MEDIUM – 04-Mar-19 12:00:00 AM
SHORT – 3/4/19 12:00 AM
There is also a DEFAULT style, which is equivalent to MEDIUM. The DateFormat class defines a number of field constants that represent the various fields in formatted date and time
strings. These field constants can create FieldPosition objects.
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 we have created a DateFormat object, we can use the setCalendar() and setTimeZone() methods if we 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 a Date object.
The DEFAULT, FULL, MEDIUM, LONG, and SHORT constants are used to 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().
The class structure for the class DateFormat is given as
public abstract class java.text.DateFormat extends java.text.Format implements java.lang.Cloneable {
// Protected Constructor
protected DateFormat();
// Format Style Constants
public static final int DEFAULT;
public static final int FULL;
public static final int LONG;
public static final int MEDIUM;
public static final int SHORT;
// Date and Time Field Constants
public static final int ERA_FIELD;
public static final int YEAR_FIELD;
public static final int MONTH_FIELD;
public static final int WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD;
public static final int DATE_FIELD, DAY_OF_YEAR_FIELD;
public static final int DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD;
public static final int TIMEZONE_FIELD;
public static final int AM_PM_FIELD;
public static final int HOUR0_FIELD, HOUR1_FIELD;
public static final int HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD;
public static final int MINUTE_FIELD;
public static final int SECOND_FIELD;
public static final int MILLISECOND_FIELD;
// Protected Instance Variables
protected Calendar calendar;
protected NumberFormat numberFormat;
// Class Methods
public static Locale[] getAvailableLocales();
public static final DateFormat getDateInstance();
public static final DateFormat getDateInstance(int style);
public static final DateFormat getDateInstance(int style, 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, 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, Locale aLocale);
// Public Instance Methods
public Object clone(); // Overrides Format
public boolean equals(Object obj); // Overrides Object
public final StringBuffer format(Object obj, StringBuffer toAppendTo,FieldPosition fieldPosition); // Defines Format
public abstract StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition fieldPosition);
public final String format(Date date);
public Calendar getCalendar();
public NumberFormat getNumberFormat();
public TimeZone getTimeZone();
public int hashCode(); // Overrides Object
public boolean isLenient();
public Date parse(String text) throws ParseException;
public abstract Date parse(String text, ParsePosition pos);
public Object parseObject(String source, ParsePosition pos); // Defines Format
public void setCalendar(Calendar newCalendar);
public void setLenient(boolean lenient);
public void setNumberFormat(NumberFormat newNumberFormat);
public void setTimeZone(TimeZone zone);
}
The details of the class structure are given as follows:
public static final int AM_PM_FIELD;
public static final int AM_PM_FIELD represents a field constant that represents the A.M./P.M. field.
public static final int DATE_FIELD;
public static final int DATE_FIELD represents a field constant that represents the date (day of the month) field.
public static final int DAY_OF_WEEK_FIELD;
public static final int DAY_OF_WEEK_FIELD represents a field constant that represents the day-of-the-week field.
public static final int DAY_OF_WEEK_IN_MONTH_FIELD;
public static final int DAY_OF_WEEK_IN_MONTH_FIELD represents a field constant that represents the day of the week in the current month field.
public static final int DAY_OF_YEAR_FIELD;
public static final int DAY_OF_YEAR_FIELD represents a field constant that represents the day-of-the-year field.
public static final int DEFAULT;
public static final int DEFAULT represents a constant that specifies the default style.
public static final int ERA_FIELD;
public static final int ERA_FIELD represents a field constant that represents the era field.
public static final int FULL;
public static final int FULL represents a constant that specifies the most complete style.
public static final int HOUR0_FIELD;
public static final int HOUR0_FIELD represents a field constant that represents the zero-based hour field.
public static final int HOUR1_FIELD;
public static final int HOUR1_FIELD represents a field constant that represents the one-based hour field.
public static final int HOUR_OF_DAY0_FIELD;
public static final int HOUR_OF_DAY0_FIELD represents a field constant that represents the zero-based hour of the day field.
public static final int HOUR_OF_DAY1_FIELD;
public static final int HOUR_OF_DAY1_FIELD represents a field constant that represents the one-based hour of the day field.
public static final int LONG;
public static final int LONG represents a constant that specifies the long style.
public static final int MEDIUM;
public static final int MEDIUM represents a constant that specifies the medium style.
public static final int MILLISECOND_FIELD;
public static final int MILLISECOND_FIELD represents a field constant that represents the millisecond field.
public static final int MINUTE_FIELD;
public static final int MINUTE_FIELD represents a field constant that represents the minute field.
public static final int MONTH_FIELD;
public static final int MONTH_FIELD represents a field constant that represents the month field.
public static final int SECOND_FIELD;
public static final int SECOND_FIELD represents a field constant that represents the second field.
public static final int SHORT;
public static final int SHORT represents a constant that specifies the short style.
public static final int TIMEZONE_FIELD;
public static final int TIMEZONE_FIELD represents a field constant that represents the time-zone field.
public static final int WEEK_OF_MONTH_FIELD;
public static final int WEEK_OF_MONTH_FIELD represents a field constant that represents the week-of-the-month field.
public static final int WEEK_OF_YEAR_FIELD;
public static final int WEEK_OF_YEAR_FIELD represents a field constant that represents the week-of-the-year field.
public static final int YEAR_FIELD;
public static final int YEAR_FIELD represents a field constant that represents the year field.
protected Calendar calendar;
protected Calendar calendar represents a Calendar object that internally generates the field values for formatting dates and times.
protected NumberFormat numberFormat;
protected NumberFormat numberFormat represents a NumberFormat object that internally formats the numbers in dates and times.
protected DateFormat();
protected DateFormat() constructor creates a DateFormat.
public static Locale[] getAvailableLocales();
public static Locale[] getAvailableLocales() method returns an array of the Locale objects for which this class can create DateFormat objects.
This method returns An array of Locale objects.
public static final DateFormat getDateInstance();
public static final DateFormat getDateInstance() method creates a DateFormat that formats and parses dates in the default locale with the default style.
This method returns a DateFormat appropriate for the default Locale that uses the default style.
public static final DateFormat getDateInstance(int style);
public static final DateFormat getDateInstance(int style) method creates a DateFormat that formats and parses dates in the default locale with the given style.
This method returns a DateFormat appropriate for the default Locale that uses the given style.
Parameter
style – A style constant.
public static final DateFormat getDateInstance(int style, Locale aLocale);
public static final DateFormat getDateInstance(int style, Locale aLocale) method creates a DateFormat that formats and parses dates in the given locale with the given style.
This method returns a DateFormat appropriate for the given Locale that uses the given style.
Parameter
style – A style constant.
aLocale – The Locale to use.
public static final DateFormat getDateTimeInstance();
public static final DateFormat getDateTimeInstance() method creates a DateFormat that formats and parses dates and times in the default locale with the default date and time styles.
This method returns a DateFormat appropriate for the default Locale that uses the default date and time styles.
public static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle);
public static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle) method creates a DateFormat that formats and parses dates and times in the default locale with the given date and time styles.
This method returns a DateFormat appropriate for the default Locale that uses the given data and time styles.
Parameter
dateStyle – A style constant.
timeStyle – A style constant.
public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale);
public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) method creates a DateFormat that formats and parses dates and times in the given locale with the given date and time styles.
This method returns a DateFormat appropriate for the given Locale that uses the given date and time styles.
Parameter
dateStyle – A style constant.
timeStyle – A style constant.
aLocale – The Locale to use.
public static final DateFormat getInstance();
public static final DateFormat getInstance() method creates a general purpose DateFormat by calling getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).
This method returns a DateFormat appropriate for the default Locale.
public static final DateFormat getTimeInstance();
public static final DateFormat getTimeInstance() method creates a DateFormat that formats and parses times in the default locale with the default style.
This method returns a DateFormat appropriate for the default Locale that uses the default style.
public static final DateFormat getTimeInstance(int style);
public static final DateFormat getTimeInstance(int style) method creates a DateFormat that formats and parses times in the default locale with the given style.
This method returns a DateFormat appropriate for the default Locale that uses the given style.
Parameter
style – A style constant.
public static final DateFormat getTimeInstance(int style, Locale aLocale);
public static final DateFormat getTimeInstance(int style, Locale aLocale) method creates a DateFormat that formats and parses times in the given locale with the given style.
This method returns a DateFormat appropriate for the given Locale that uses the given style.
Parameter
style – A style constant.
aLocale – The Locale to use.
public Object clone();
public Object clone() method creates a copy of this DateFormat and returns it.
This method returns a copy of this DateFormat.
public boolean equals(Object obj);
public boolean equals(Object obj) method returns true if obj is an instance of DateFormat and is equivalent to this DateFormat.
This method returns true if the objects are equal; false if they are not.
Parameter
obj – The object to be compared with this object.
public final String format(Date date);
public final String format(Date date) method formats the given date and returns the result as a string.
This method returns a string that contains a formatted representation of the date.
Parameter
date – The Date object to be formatted.
public final StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition);
public final StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) method formats the given object and appends the result to the given StringBuffer. If fieldPosition refers to one of the time or date fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.
This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.
Parameter
obj – The object to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
fieldPosition – A date or time field.
public abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition);
public abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) method formats the given date and appends the result to the given StringBuffer. If fieldPosition refers to one of the time or date fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.
This method returns the given buffer toAppendTo with the formatted representation of the date appended to it.
Parameter
date – The Date object to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
fieldPosition – A date or time field.
public Calendar getCalendar();
public Calendar getCalendar() method returns the Calendar object that this DateFormat uses internally.
This method returns the internal Calendar object of this DateFormat.
public NumberFormat getNumberFormat();
public NumberFormat getNumberFormat() method returns the NumberFormat object that this DateFormat uses internally.
This method returns the internal NumberFormat object of this DateFormat.
public TimeZone getTimeZone();
public TimeZone getTimeZone() method returns the TimeZone object that this DateFormat uses internally.
This method returns the internal TimeZone object of this DateFormat.
public int hashCode();
public int hashCode() method returns a hashcode for this DateFormat.
This method returns a hashcode for this object.
public boolean isLenient();
public boolean isLenient() method returns the current leniency of this DateFormat. A value of false indicates that the DateFormat throws exceptions when it tries to parse questionable data, while a value of true indicates that the DateFormat makes its best guess to interpret questionable data. For example, if the DateFormat is being lenient, a date such as March 135, 1997 is interpreted as the 135th day after March 1, 1997.
This method returns a boolean value that indicates the leniency of this DateFormat.
public Date parse(String text);
public Date parse(String text) method parses a date from the given string, starting from the beginning of the string.
This method returns the Date object represented by the given string.
Parameter
text – The string to be parsed.
public abstract Date parse(String text, ParsePosition pos);
public abstract Date parse(String text, ParsePosition pos) method parses a date from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.
This method returns the Date object represented by the text starting at the given position.
Parameter
text – The string to be parsed.
pos – A ParsePosition object that can specify a position in the string.
public Object parseObject(String source, ParsePosition pos);
public Object parseObject(String source, ParsePosition pos) method parses a date from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.
This method returns the object represented by the text starting at the given position.
Parameter
source – The string to be parsed.
pos – A ParsePosition object that can specify a position in the string.
public void setCalendar(Calendar newCalendar);
public void setCalendar(Calendar newCalendar) method sets the Calendar that this DateFormat uses internally.
Parameter
newCalendar – The new Calendar to use.
public void setLenient(boolean lenient);
public void setLenient(boolean lenient) method sets the leniency of this DateFormat. A value of false specifies that the DateFormat throws exceptions when it tries to parse questionable data, while a value of true indicates that the DateFormat makes its best guess to interpret questionable data. For example, if the Calendar is being lenient, a date such as March 135,1997 is interpreted as the 135th day after March 1, 1997.
Parameter
lenient – A boolean value that specifies the leniency of this DateFormat.
public void setNumberFormat(NumberFormat newNumberFormat);
public void setNumberFormat(NumberFormat newNumberFormat) method sets the NumberFormat that this DateFormat uses internally.
Parameter
newNumberFormat – The new NumberFormat to use.
public void setTimeZone(TimeZone zone);
public void setTimeZone(TimeZone zone) method sets the TimeZone that this DateFormat uses internally.
Parameter
zone – The new TimeZone to use.
Apart from these DateFormat class also has inherited methods from class- Object. They are as follows:
- finalize()
- notifyAll()
- wait()
- wait(long, int)
- getClass()
- notify()
- toString()
- wait(long)
DateFormat class also has inherited a methods from Format. That is as follows:
- format(obj)