DataFormat class

DateFormat is an abstract class for date/time formatting.

DateFormat is an abstract class that provides the ability to format and parse dates and times.

The getDateInstance( ) method returns an instance of DateFormat that can format date information.

It is available in these forms:


static final DateFormat getDateInstance( ) 
static final DateFormat getDateInstance(int style) 
static final DateFormat getDateInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat.

The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used.

format( ) method has several overloaded forms, one of which is shown here:


final String format(Date d)

The argument is a Date object that is to be displayed. The method returns a string containing the formatted information.


// Demonstrate date formats. 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String args[]) {
    Date date = new Date();
    DateFormat df;
    df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN);
    System.out.println("Japan: " + df.format(date));
    df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA);
    System.out.println("Korea: " + df.format(date));
    df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
    System.out.println("United Kingdom: " + df.format(date));
    df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
    System.out.println("United States: " + df.format(date));
  }
}

The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions:


static final DateFormat getTimeInstance( ) 
static final DateFormat getTimeInstance(int style) 
static final DateFormat getTimeInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat.

The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used.

Useful contants

static int AM_PM_FIELD
Useful constant for AM_PM field alignment.
static int DATE_FIELD
Useful constant for DATE field alignment.
static int DAY_OF_WEEK_FIELD
Useful constant for DAY_OF_WEEK field alignment.
static int DAY_OF_WEEK_IN_MONTH_FIELD
Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
static int DAY_OF_YEAR_FIELD
Useful constant for DAY_OF_YEAR field alignment.
static int ERA_FIELD
Useful constant for ERA field alignment.
static int HOUR_OF_DAY0_FIELD
Useful constant for zero-based HOUR_OF_DAY field alignment.
static int HOUR_OF_DAY1_FIELD
Useful constant for one-based HOUR_OF_DAY field alignment.
static int HOUR0_FIELD
Useful constant for zero-based HOUR field alignment.
static int HOUR1_FIELD
Useful constant for one-based HOUR field alignment.
static int MILLISECOND_FIELD
Useful constant for MILLISECOND field alignment.
static int MINUTE_FIELD
Useful constant for MINUTE field alignment.
static int MONTH_FIELD
Useful constant for MONTH field alignment.
static int SECOND_FIELD
Useful constant for SECOND field alignment.
static int TIMEZONE_FIELD
Useful constant for TIMEZONE field alignment.
static int WEEK_OF_MONTH_FIELD
Useful constant for WEEK_OF_MONTH field alignment.
static int WEEK_OF_YEAR_FIELD
Useful constant for WEEK_OF_YEAR field alignment.
static int YEAR_FIELD
Useful constant for YEAR field alignment.
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.

Format a date

String format(Date date)
Formats a Date into a date/time string.
StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)
Overrides Format.

Create new DateFormat instance

Calendar getCalendar()
Gets the calendar associated with this date/time formatter.
static DateFormat getDateInstance()
Gets the date formatter with the default formatting style for the default locale.
static DateFormat getDateInstance(int style)
Gets the date formatter with the given formatting style for the default locale.
static DateFormat getDateInstance(int style, Locale aLocale)
Gets the date formatter with the given formatting style for the given locale.
static DateFormat getDateTimeInstance()
Gets the date/time formatter with the default formatting style for the default locale.
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Gets the date/time formatter with the given date and time formatting styles for the default locale.
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
Gets the date/time formatter with the given formatting styles for the given locale.
static DateFormat getInstance()
Get a default date/time formatter that uses the SHORT style for both the date and the time.
NumberFormat getNumberFormat()
Gets the number formatter which this date/time formatter uses to format and parse a time.
static DateFormat getTimeInstance()
Gets the time formatter with the default formatting style for the default locale.
static DateFormat getTimeInstance(int style)
Gets the time formatter with the given formatting style for the default locale.
static DateFormat getTimeInstance(int style, Locale aLocale)
Gets the time formatter with the given formatting style for the given locale.
TimeZone getTimeZone()
Gets the time zone.

Get all locales

static Locale[] getAvailableLocales()
Returns an array of all locales for which the get*Instance methods of this class can return localized instances.

Is date/time parsed lenient

boolean isLenient()
Tell whether date/time parsing is to be lenient.

Parse string to get Date

Date parse(String source)
Parses text from the beginning of the given string to produce a date.
Object parseObject(String source, ParsePosition pos)
Parses text from a string to produce a Date.

Change the date formatter

void setCalendar(Calendar newCalendar)
Set the calendar to be used by this date format.
void setLenient(boolean lenient)
Specify whether or not date/time parsing is to be lenient.
void setNumberFormat(NumberFormat newNumberFormat)
Allows you to set the number formatter.
void setTimeZone(TimeZone zone)
Sets the time zone for the calendar of this DateFormat object.

Revised from Open JDK source code

Home 
  Java Book 
    Essential Classes  

DataFormat:
  1. DataFormat class
  2. Create new DateFormat instance
  3. Constants from DateFormat
  4. Format a date
  5. Get all locales
  6. Parse string to get Date