DateFormat (Java SE 9 & JDK 9 ) (original) (raw)


public abstract class DateFormat
extends Format
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 provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions.
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.

 
DateFormat df = DateFormat.getDateInstance();  
for (int i = 0; i < myDate.length; ++i) {  
    output.println(df.format(myDate[i]) + "; ");  
}  
 

To format a date for a different Locale, specify it in the call to getDateInstance().

 
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:

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.
Implementation Requirements: