Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

DateFormat getDateTimeInstance() Method in Java with Examples

  • Last Updated : 24 Jan, 2022

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);
    }
}
Output
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);
    }
}
Output
The Original: 01/11/2022
Object: java.text.SimpleDateFormat@f95cf381
Jan 11, 2022, 6:30:09 AM

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!