DateFormat class of java.text package 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.
Note: 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. Â
Package-view:
java.text Package DateFormat Class getDateTimeInstance() Method
getDateTimeInstance() Method of DateFormat class in Java is used to get the Date/Time formatter. This Date/Time formatter comes with the default formatting style for a default locale.
Syntax:Â Â
public static final DateFormat getDateTimeInstance()
Return Type: The method returns a date/time formatter in a particular format.
Example 1:
Java
// Java Program to Illustrate the // getDateTimeInstance() Method // of DateFormat Class Â
// Importing required classes import java.text.*; import java.util.*; Â
// Main class public class GFG { Â
    // Main driver method     public static void main(String[] argv)     { Â
        // Initializing the first formatter by         // creating object of DateFormat class         DateFormat DFormat = DateFormat.getDateTimeInstance(); Â
        System.out.println( "Object: " + DFormat); Â
        // Formatting the string using format() method         String str = DFormat.format( new Date()); Â
        // Printing the formatted string time         System.out.println(str);     } } |
Object: java.text.SimpleDateFormat@f95cf381 Jan 11, 2022, 6:25:40 AM
Example 2:
Java
// Java Program to Illustrate the // getDateTimeInstance() Method // of DateFormat Class Â
// Importing required classes import java.text.*; import java.util.*; Â
// Main class public class GFG { Â
    // Main driver method     public static void main(String[] args)         throws InterruptedException     { Â
        // Initializing SimpleDateFormat         SimpleDateFormat SDFormat             = new SimpleDateFormat( "MM/dd/yyyy" ); Â
        // Displaying the formats         Date date = new Date();         String str_Date1 = SDFormat.format(date); Â
        // Displaying the unformatted date         System.out.println( "The Original: " + (str_Date1)); Â
        // Initializing the Time formatter         DateFormat DFormat = DateFormat.getDateTimeInstance();         System.out.println( "Object: " + DFormat); Â
        // Formatting the string         String str = DFormat.format( new Date()); Â
        // Displaying the string time on console         System.out.println(str);     } } |
The Original: 01/11/2022 Object: java.text.SimpleDateFormat@f95cf381 Jan 11, 2022, 6:30:09 AM