public abstract class DateFormat extends Object
DateFormat
is 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 as SimpleDateFormat
, allows for
formatting (i.e., date → text), parsing (text → date), and normalization. The date is represented as a
Date
object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
DateFormat
helps 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 getDateInstance
to get the normal date format for that country. There are other static factory methods
available. Use getTimeInstance
to get the time format for that country. Use getDateTimeInstance
to
get a date and time format. You can pass in different options to these factory methods to control the length of the
result; from SHORT
to MEDIUM
to LONG
to FULL
. The exact result depends on the
locale, but generally:
SHORT
is completely numeric, such as 12.13.52
or 3:30pm
MEDIUM
is longer, such as Jan 12, 1952
LONG
is longer, such as January 12, 1952
or 3:30:32pm
FULL
is pretty completely specified, such as Tuesday, 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 DateFormat
you get from the factory
methods to a SimpleDateFormat
. This will work for the majority of countries; just remember to put it in a
try
block in case you encounter an unusual one.
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.
SimpleDateFormat
,
Calendar
,
TimeZone
Modifier and Type | Field and Description |
---|---|
protected Calendar |
calendar
The
Calendar instance used for calculating the date-time fields and the instant of time. |
static int |
DEFAULT
Constant for default style pattern.
|
static int |
FULL
Constant for full style pattern.
|
static int |
LONG
Constant for long style pattern.
|
static int |
MEDIUM
Constant for medium style pattern.
|
static int |
SHORT
Constant for short style pattern.
|
Modifier | Constructor and Description |
---|---|
protected |
DateFormat()
Create a new date format.
|
Modifier and Type | Method and Description |
---|---|
abstract String |
format(Date date)
Formats a Date into a date/time string.
|
Calendar |
getCalendar()
Gets the calendar associated with this date/time formatter.
|
static DateFormat |
getDateInstance()
Gets the date formatter with the default formatting style.
|
static DateFormat |
getDateInstance(int style)
Gets the date formatter with the given formatting style.
|
static DateFormat |
getDateTimeInstance()
Gets the date/time formatter with the default formatting style.
|
static DateFormat |
getDateTimeInstance(int dateStyle,
int timeStyle)
Gets the date/time formatter with the given date and time formatting styles.
|
static DateFormat |
getInstance()
Get a default date/time formatter that uses the SHORT style for both the date and the time.
|
static DateFormat |
getTimeInstance()
Gets the time formatter with the default formatting style.
|
static DateFormat |
getTimeInstance(int style)
Gets the time formatter with the given formatting style.
|
void |
setCalendar(Calendar newCalendar)
Set the calendar to be used by this date format.
|
protected Calendar calendar
Calendar
instance used for calculating the date-time fields and the instant of time. This field is
used for both formatting and parsing.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
public abstract String format(Date date)
date
- the time value to be formatted into a time string.public Calendar getCalendar()
public static final DateFormat getDateInstance()
public static final DateFormat getDateInstance(int style)
style
- the given formatting style. For example, SHORT for "M/d/yy".public static final DateFormat getDateTimeInstance()
public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
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".public static final DateFormat getInstance()
public static final DateFormat getTimeInstance()
public static final DateFormat getTimeInstance(int style)
style
- the given formatting style. For example, SHORT for "h:mm a".public void setCalendar(Calendar newCalendar)
newCalendar
- the new Calendar
to be used by the date format