Class DateFormat
- java.lang.Object
-
- java.text.DateFormat
-
- Direct Known Subclasses:
SimpleDateFormat
public abstract class DateFormat extends Object
DateFormatis an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such asSimpleDateFormat, allows for formatting (i.e., date → text), parsing (text → date), and normalization. The date is represented as aDateobject or as the milliseconds since January 1, 1970, 00:00:00 GMT.DateFormathelps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.To format a date for the current Locale, use one of the static factory methods:
myString = DateFormat.getDateInstance().format(myDate);If you are formatting multiple dates, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.
{ @code DateFormat df = DateFormat.getDateInstance(); for (int i = 0; i < myDate.length; ++i) { output.println(df.format(myDate[i]) + "; "); } }{ @code DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); }You can use a DateFormat to parse also.
myDate = df.parse(myString);Use
getDateInstanceto get the normal date format for that country. There are other static factory methods available. UsegetTimeInstanceto get the time format for that country. UsegetDateTimeInstanceto get a date and time format. You can pass in different options to these factory methods to control the length of the result; fromSHORTtoMEDIUMtoLONGtoFULL. The exact result depends on the locale, but generally:SHORTis completely numeric, such as12.13.52or3:30pmMEDIUMis longer, such asJan 12, 1952LONGis longer, such asJanuary 12, 1952or3:30:32pmFULLis pretty completely specified, such asTuesday, April 12, 1952 AD or 3:30:42pm PST.
You can also set the time zone on the format if you wish. If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the
DateFormatyou get from the factory methods to aSimpleDateFormat. This will work for the majority of countries; just remember to put it in atryblock in case you encounter an unusual one.Synchronization
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
- See Also:
SimpleDateFormat,Calendar,TimeZone
-
-
Field Summary
Fields Modifier and Type Field Description protected CalendarcalendarTheCalendarinstance used for calculating the date-time fields and the instant of time.static intDEFAULTConstant for default style pattern.static intFULLConstant for full style pattern.static intLONGConstant for long style pattern.static intMEDIUMConstant for medium style pattern.static intSHORTConstant for short style pattern.
-
Constructor Summary
Constructors Modifier Constructor Description protectedDateFormat()Create a new date format.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Stringformat(Date date)Formats a Date into a date/time string.CalendargetCalendar()Gets the calendar associated with this date/time formatter.static DateFormatgetDateInstance()Gets the date formatter with the default formatting style.static DateFormatgetDateInstance(int style)Gets the date formatter with the given formatting style.static DateFormatgetDateTimeInstance()Gets the date/time formatter with the default formatting style.static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle)Gets the date/time formatter with the given date and time formatting styles.static DateFormatgetInstance()Get a default date/time formatter that uses the SHORT style for both the date and the time.static DateFormatgetTimeInstance()Gets the time formatter with the default formatting style.static DateFormatgetTimeInstance(int style)Gets the time formatter with the given formatting style.voidsetCalendar(Calendar newCalendar)Set the calendar to be used by this date format.
-
-
-
Field Detail
-
FULL
public static final int FULL
Constant for full style pattern.- See Also:
- Constant Field Values
-
LONG
public static final int LONG
Constant for long style pattern.- See Also:
- Constant Field Values
-
MEDIUM
public static final int MEDIUM
Constant for medium style pattern.- See Also:
- Constant Field Values
-
SHORT
public static final int SHORT
Constant for short style pattern.- See Also:
- Constant Field Values
-
DEFAULT
public static final int DEFAULT
Constant for default style pattern. Its value is MEDIUM.- See Also:
- Constant Field Values
-
-
Method Detail
-
format
public abstract String format(Date date)
Formats a Date into a date/time string.- Parameters:
date- the time value to be formatted into a time string.- Returns:
- the formatted time string.
-
getTimeInstance
public static final DateFormat getTimeInstance()
Gets the time formatter with the default formatting style.- Returns:
- a time formatter.
-
getTimeInstance
public static final DateFormat getTimeInstance(int style)
Gets the time formatter with the given formatting style.- Parameters:
style- the given formatting style. For example, SHORT for "h:mm a".- Returns:
- a time formatter.
-
getDateInstance
public static final DateFormat getDateInstance()
Gets the date formatter with the default formatting style.- Returns:
- a date formatter.
-
getDateInstance
public static final DateFormat getDateInstance(int style)
Gets the date formatter with the given formatting style.- Parameters:
style- the given formatting style. For example, SHORT for "M/d/yy".- Returns:
- a date formatter.
-
getDateTimeInstance
public static final DateFormat getDateTimeInstance()
Gets the date/time formatter with the default formatting style.- Returns:
- a date/time formatter.
-
getDateTimeInstance
public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Gets the date/time formatter with the given date and time formatting styles.- Parameters:
dateStyle- the given date formatting style. For example, SHORT for "M/d/yy".timeStyle- the given time formatting style. For example, SHORT for "h:mm a".- Returns:
- a date/time formatter.
-
getInstance
public static final DateFormat getInstance()
Get a default date/time formatter that uses the SHORT style for both the date and the time.- Returns:
- a date/time formatter
-
setCalendar
public void setCalendar(Calendar newCalendar)
Set the calendar to be used by this date format. Initially, the default calendar.- Parameters:
newCalendar- the newCalendarto be used by the date format
-
getCalendar
public Calendar getCalendar()
Gets the calendar associated with this date/time formatter.- Returns:
- the calendar associated with this date/time formatter.
-
-