Skip to content
Related Articles

Related Articles

DateFormat getDateInstance() Method in Java with Examples

Improve Article
Save Article
  • Last Updated : 24 Jan, 2022
Improve Article
Save Article

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
        getDateInstance() Method

getDateInstance() Method of DateFormat class in Java is used to get the date formatter. This date formatter comes with the default formatting style for a default locale.

Syntax: 

public static final DateFormat getDateInstance()

Return Type: Returns a date formatted in a particular format.

Example 1:

Java




// Java Program to Illustrate getDateInstance() 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
        // using getDateInstance() method
        DateFormat DFormat = DateFormat.getDateInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Printing the string date on console
        System.out.println(str);
    }
}

Output: 

Object: java.text.SimpleDateFormat@ce9bf0a5
Mar 27, 2019

 

Example 2:

Java




// Java Program to Illustrate getDateInstance() 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 by
        // creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Printing the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying  the original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Date formatter
        DateFormat DFormat = DateFormat.getDateInstance();
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Printing the string date on console
        System.out.println(str);
    }
}

Output

The Original: 01/11/2022
Object: java.text.SimpleDateFormat@ad508834
Jan 11, 2022

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!