Saturday, October 25, 2025
HomeLanguagesJavaJava DateFormat getInstance() Method with Examples

Java DateFormat getInstance() Method with Examples

The getInstance() method of DateFormat class will return the date and time formatter with the default formatting style for the default locale.

Syntax:

public static final DateFormat getInstance()

Parameter: This method doesn’t require any parameters.

Return value: This method will return the date format in a particular format.

Example 1:

Java




// Java program to illustrate
// GetInstance() method
 
// importing the required packages
import java.text.DateFormat;
import java.util.Date;
 
class Testclass {
    public static void main(String[] args)
    {
 
        // initializing the Date
        Date d = new Date();
 
        // initializing the DateFormat using getInstance()
        DateFormat df = DateFormat.getInstance();
 
        // printing the DateFormat return value as object
        System.out.println("DateFormat Object : " + df);
 
        // formatting the current date into a string
        String str = df.format(d);
 
        // printing the current date
        System.out.println("Current date : " + str);
    }
}


Output

DateFormat Object : java.text.SimpleDateFormat@c88bcc54
Current date : 12/15/21, 8:23 AM

We can also display the date in the required format using SimpleDateFormat class.

Below is the example showing the use of SimpleDateFormat 

Example 2:

Java




// Java program to illustrate
// GetInstance() method
 
// importing the required packages
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
class Testclass {
    public static void main(String[] args)
    {
        // initializing the Date
        Date d = new Date();
 
        // initializing the DateFormat using getInstance()
        DateFormat df = DateFormat.getInstance();
 
        // printing the DateFormat return value as object
        System.out.println("DateFormat Object : " + df);
 
        // formatting the current date into a string
        String str = df.format(d);
 
        // printing the current date
        System.out.println("Current date : " + str);
 
        // initializing the SimpleDateFormat
        SimpleDateFormat sdf
            = new SimpleDateFormat("MM-dd-yyyy");
 
        // formatting the SimpleDateFormat into a string
        String st = sdf.format(d);
 
        // printing the formatted value
        System.out.println("Formatted Date : " + st);
    }
}


Output

DateFormat Object : java.text.SimpleDateFormat@c88bcc54
Current date : 12/15/21, 8:26 AM
Formatted Date : 12-15-2021
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS