[ Web Proxy ]
URL:
Viewing: https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/text/DateFormat.html [Back]  [Original]

DateFormat (Java SE 24 & JDK 24)
Contents 
Hide sidebar ❮❯ Show sidebar
  1. Description
    1. Synchronization
  2. Nested Class Summary
  3. Field Summary
  4. Constructor Summary
  5. Method Summary
  6. Field Details
    1. calendar
    2. numberFormat
    3. ERA_FIELD
    4. YEAR_FIELD
    5. MONTH_FIELD
    6. DATE_FIELD
    7. HOUR_OF_DAY1_FIELD
    8. HOUR_OF_DAY0_FIELD
    9. MINUTE_FIELD
    10. SECOND_FIELD
    11. MILLISECOND_FIELD
    12. DAY_OF_WEEK_FIELD
    13. DAY_OF_YEAR_FIELD
    14. DAY_OF_WEEK_IN_MONTH_FIELD
    15. WEEK_OF_YEAR_FIELD
    16. WEEK_OF_MONTH_FIELD
    17. AM_PM_FIELD
    18. HOUR1_FIELD
    19. HOUR0_FIELD
    20. TIMEZONE_FIELD
    21. FULL
    22. LONG
    23. MEDIUM
    24. SHORT
    25. DEFAULT
  7. Constructor Details
    1. DateFormat()
  8. Method Details
    1. format(Object, StringBuffer, FieldPosition)
    2. format(Date, StringBuffer, FieldPosition)
    3. format(Date)
    4. parse(String)
    5. parse(String, ParsePosition)
    6. parseObject(String, ParsePosition)
    7. getTimeInstance()
    8. getTimeInstance(int)
    9. getTimeInstance(int, Locale)
    10. getDateInstance()
    11. getDateInstance(int)
    12. getDateInstance(int, Locale)
    13. getDateTimeInstance()
    14. getDateTimeInstance(int, int)
    15. getDateTimeInstance(int, int, Locale)
    16. getInstance()
    17. getAvailableLocales()
    18. setCalendar(Calendar)
    19. getCalendar()
    20. setNumberFormat(NumberFormat)
    21. getNumberFormat()
    22. setTimeZone(TimeZone)
    23. getTimeZone()
    24. setLenient(boolean)
    25. isLenient()
    26. hashCode()
    27. equals(Object)
    28. clone()

Class DateFormat

java.lang.Object
java.text.Format
java.text.DateFormat
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
SimpleDateFormat

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 static factory 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. For any of the factory methods with the parameter style, an IllegalArgumentException will be thrown if style is not equal to any of the defined formatting styles. 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:

CopyCopy snippet [Copy snippet]
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.

CopyCopy snippet [Copy snippet]
DateFormat df = DateFormat.getDateInstance();
for (Date myDate : dates) {
    output.println(df.format(myDate) + "; ");
}

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

CopyCopy snippet [Copy snippet]
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

If the specified locale contains "ca" (calendar), "rg" (region override), and/or "tz" (timezone) Unicode extensions, the calendar, the country and/or the time zone for formatting are overridden. If both "ca" and "rg" are specified, the calendar from the "ca" extension supersedes the implicit one from the "rg" extension.

You can use a DateFormat to parse also.

CopyCopy snippet [Copy snippet]
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 the shortest and mainly numeric, such as 12.13.52 or 3:30pm
  • MEDIUM is longer, such as Jan 12, 1952
  • LONG is even longer, such as January 12, 1952 or 3:30:32pm
  • FULL is the longest, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
For those fields with text, typically abbreviated text form is used with MEDIUM option, and full text form is used with LONG and FULL options.

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.

You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to

  • progressively parse through pieces of a string.
  • align any particular field, or find out where it is for selection on the screen.

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.

API Note:
Consider using DateTimeFormatter as an immutable and thread-safe alternative.
Implementation Requirements:
Since:
1.1
See Also:


Web Proxy Viewer  |  New URL  |  Original Page