DateFormat, SimpleDateFormat class in java.text package: constructor, methods

DateFormat class: DateFormat class is an abstract class defined by java.text. It is used for formatting and parsing date and time.

Methods defined by DateFormat class:

format( ) method: It is used to return a string having formatted information.


final String format(Date d)

getDateInstance( ) method: It is used to format date information.


static final DateFormat getDateInstance( )

By using style. It can have values: FULL, SHORT, MEDIUM, DEFAULT, or LONG:

static final DateFormat getDateInstance(int style)

By using static referenc locale:

static final DateFormat getDateInstance(int style, Locale locale)

SimpleDateFormat class: It is used for custom formatting of date and time. It is a sub class of DateFormat class.

Constructor defined by SimpleDateFormat class:

SimpleDateFormat(String fstr)

where, fstr specifies the way date and time will be displayed.

For Example:

SimpleDateFormat sdf = SimpleDateFormat(“MM dd yyyy hh:mm “);

Following are the symbols which can be used in format string:

  • D: Day of a year (1–366).
  • E: Day of a week.
  • F: Day of a week in month.
  • G: Era (AD or BC).
  • H: Hour in a day (0–23).
  • K: Hour in AM/PM (0–11).
  • M: Month.
  • S: Millisecond in second.
  • W: Week of month (1–5)
  • Z: Time zone in RFC822 format.
  • a: AM/PM.
  • d: Day of a month (1–31).
  • h: Hour in AM/PM (1–12).
  • k: Hour in a day (1–24).
  • m: Minute in an hour (0–59).
  • s: Second in a minute (0–59).
  • w: Week of year (1–52).
  • y: Year.
  • z: Time zone.

Leave a Reply