DateFormat Class in Java

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
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
Example: Java Program for better understanding of DateFormat class
Java
// import java.text package for using DateFormat classimport 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));    }}
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
Java-Classes
Java-DateFormat
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java