java.text.DateFormat class

java.text.DateFormat

The DateFormat is an abstract class used for date time formatting. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.

DateFormat Fields:

protected Calendar calendar  
protected NumberFormat numberFormat  
public static final int ERA_FIELD  
public static final int YEAR_FIELD  
public static final int MONTH_FIELD  
public static final int DATE_FIELD  
public static final int HOUR_OF_DAY1_FIELD  
public static final int HOUR_OF_DAY0_FIELD  
public static final int MINUTE_FIELD  
public static final int SECOND_FIELD  
public static final int MILLISECOND_FIELD  
public static final int DAY_OF_WEEK_FIELD  
public static final int DAY_OF_YEAR_FIELD  
public static final int DAY_OF_WEEK_IN_MONTH_FIELD  
public static final int WEEK_OF_YEAR_FIELD  
public static final int WEEK_OF_MONTH_FIELD  
public static final int AM_PM_FIELD  
public static final int HOUR1_FIELD  
public static final int HOUR0_FIELD  
public static final int TIMEZONE_FIELD  
public static final int FULL  
public static final int LONG  
public static final int MEDIUM  
public static final int SHORT  
public static final int DEFAULT

Java DateFormat Class Methods

Method Description
final String format(Date date) It converts given Date object into string.
Date parse(String source)throws ParseException It converts string into Date object.
static final DateFormat getTimeInstance() It returns time formatter with default formatting style for the default locale.
static final DateFormat getTimeInstance(int style) It returns time formatter with the given formatting style for the default locale.
static final DateFormat getTimeInstance(int style, Locale locale) It returns time formatter with the given formatting style for the given locale.
static final DateFormat getDateInstance() It returns date formatter with default formatting style for the default locale.
static final DateFormat getDateInstance(int style) It returns date formatter with the given formatting style for the default locale.
static final DateFormat getDateInstance(int style, Locale locale) It returns date formatter with the given formatting style for the given locale.
static final DateFormat getDateTimeInstance() It returns date/time formatter with default formatting style for the default locale.
static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle) It returns date/time formatter with the given date formatting style and time formatting style for the default locale.
static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) It returns date/time formatter with the given It date formatting style and time formatting style for the given locale.
static final DateFormat getInstance() It returns date/time formatter with short formatting style for date and time.
static Locale[] getAvailableLocales() It returns an array of available locales.
Calendar getCalendar() It returns an instance of Calendar for this DateFormat instance.
NumberFormat getNumberFormat() It returns an instance of NumberFormat for this DateFormat instance.
TimeZone getTimeZone() It returns an instance of TimeZone for this DateFormat instance.

java.text.DateFormat class example

package com.w3schools;
 
import java.text.DateFormat;
import java.util.Date;
 
public class DateFormatTest {
    public static void main(String args[]){
	Date currentDate = new Date();  
        System.out.println("Current Date: "+currentDate);  
        String dateStaing = DateFormat.getInstance().format(currentDate);  
        System.out.println("Date Format using getInstance(): "+dateStaing);  
    }
}

Output:

Current Date: Mon Apr 09 18:43:59 IST 2018
Date Format using getInstance(): 9/4/18 6:43 PM