Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

DateFormat Class in Java

  • Last Updated : 15 Dec, 2021

The java.text.DateFormat is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time.

DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.  

Class Signature:

public abstract class DateFormat extends Format

Constructor 

DateFormat(): The DateFormat() is the constructor of the DateFormat class that is called when an instance of an object of DateFormat class is created.

Note: DateFormat is an abstract class therefore cannot be instantiated

Fields in DateFormat 

S.NO.Field                                                                                                  Data Type                                                      
1.AM_PM_FIELDstatic int
2.calendarprotected Calendar
3.DATE_FIELDstatic int
4.DAY_OF_WEEK_FIELDstatic int
5.DAY_OF_WEEK_IN_MONTH_FIELDstatic int
6.DAY_OF_YEAR_FIELDstatic int
7. DEFAULTstatic int
8.ERA_FIELDstatic int
9.FULLstatic int
10.HOUR_OF_DAY0_FIELDstatic int
11.HOUR_OF_DAY1_FIELDstatic int
12.HOUR0_FIELDstatic int
13.HOUR1_FIELDstatic int
14.LONGstatic int
15.MEDIUMstatic int
16.MILLISECOND_FIELDstatic int
17.MINUTE_FIELDstatic int
18.MONTH_FIELDstatic int
19.numberFormatprotected NumberFormat
20.SECOND_FIELDstatic int
21.SHORTstatic int
22.TIMEZONE_FIELDstatic int
23. WEEK_OF_MONTH_FIELDstatic int
24. WEEK_OF_YEAR_FIELDstatic int
25.YEAR_FIELDstatic int

Methods of DateFormat Class

S.No.     Method                                     Description                                         Return Type                       
1.format(Date date)This method formats a Date into a date/time stringString
2.format(Date date, StringBuffer toAppendTo, FieldPosition position)This method formats a Date into a date/time string.StringBuffer
3, getCalendar()This method returns the calendar associated with this date/time formatterCalendar
4.getDateInstance()This method returns the date formatter with the default formatting style for the default localestatic DateFormat
5.getDateInstance(int style)This method returns the date formatter with the given formatting style for the given locale.static DateFormat
6.getDateTimeInstance()This method returns the date/time formatter with the default formatting style for the default locale.static DateFormat
7.getDateTimeInstance(int dateStyle, int timeStyle)This method returns the date/time formatter with the given date and time formatting styles for the default localestatic DateFormat
8.getInstance()This method returns the default date/time formatter that uses the SHORT style for both the date and the time.static DateFormat
9.getTimeInstance()This method returns the time formatter with the default formatting style for the default locale.static DateFormat
10.getTimeInstance(int style)This method returns the time formatter with the given formatting style for the default locale.static DateFormat
11. getTimeInstance(int style, Locale aLocale)This method returns the time formatter with the given formatting style for the given locale.static DateFormat
12.getNumberFormat()This method returns the number formatted, which this date/time formatter uses to format and parse a time.NumberFormat
13.parse(String source)This method parses text from the beginning of the given string to produce a date.Date
14.parse(String source, ParsePosition pos)This method parses a date/time string according to the given parse position.Date
15.parseObject(String source, ParsePosition pos)This method parses text from a string to produce a Date.Object
16.setCalendar(Calendar calendar)This method sets the calendar to be used by this date format.void
17.setLenient(boolean lenient)This method specifies whether or not date/time parsing is to be lenient.void
18.setTimeZone(TimeZone zone)This method sets the time zone for the calendar of this DateFormat object.void
19.getTimeZone()This method returns the time zoneTimeZone

Example: Java Program for better understanding of DateFormat class

Java




// import java.text package for using DateFormat class
import java.text.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of Date class
        Date date = new Date();
        // to format a date for our timezone
        System.out.println(
            "Local Date and Time is : "
            + DateFormat.getInstance().format(date));
  
        // to format a date for different locale
        System.out.println(
            "Date of Canada region : "
            + DateFormat
                  .getDateInstance(DateFormat.LONG,
                                   Locale.CANADA)
                  .format(date));
        System.out.println(
            "Time of Canada region : "
            + DateFormat
                  .getTimeInstance(DateFormat.LONG,
                                   Locale.CANADA)
                  .format(date));
        System.out.println(
            "Date of Italy region : "
            + DateFormat
                  .getDateInstance(DateFormat.LONG,
                                   Locale.ITALY)
                  .format(date));
        System.out.println(
            "Time of Italy region : "
            + DateFormat
                  .getTimeInstance(DateFormat.LONG,
                                   Locale.ITALY)
                  .format(date));
  
        // to get the time zone
        System.out.println(
            "Time zone is : "
            + DateFormat.getInstance().getTimeZone());
        System.out.println(
            "Local Date and Time using getDateInstance() : "
            + DateFormat.getDateInstance().format(date));
        System.out.println(
            "Local Date and Time using getTimeInstance() : "
            + DateFormat.getTimeInstance().format(date));
        System.out.println(
            "Local Date and Time using getDateTimeInstance() : "
            + DateFormat.getDateTimeInstance().format(
                date));
        System.out.println(
            "Local Date and Time using getDateInstance(DateFormat.LONG) : "
            + DateFormat.getTimeInstance(DateFormat.LONG)
                  .format(date));
        System.out.println(
            "Local Date and Time using getDateInstance(DateFormat.MEDIUM) : "
            + DateFormat.getTimeInstance(DateFormat.MEDIUM)
                  .format(date));
        System.out.println(
            "Local Date and Time using getDateInstance(DateFormat.SHORT) : "
            + DateFormat.getTimeInstance(DateFormat.SHORT)
                  .format(date));
    }
}
Output
Local Date and Time is : 11/28/21, 7:17 PM
Date of Canada region : November 28, 2021
Time of Canada region : 7:17:40 p.m. UTC
Date of Italy region : 28 novembre 2021
Time of Italy region : 19:17:40 UTC
Time zone is : sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Local Date and Time using getDateInstance() : Nov 28, 2021
Local Date and Time using getTimeInstance() : 7:17:40 PM
Local Date and Time using getDateTimeInstance() : Nov 28, 2021, 7:17:40 PM
Local Date and Time using getDateInstance(DateFormat.LONG) : 7:17:40 PM UTC
Local Date and Time using getDateInstance(DateFormat.MEDIUM) : 7:17:40 PM
Local Date and Time using getDateInstance(DateFormat.SHORT) : 7:17 PM

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!