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
import java.text.*;
import java.util.*;
public class GFG {
public static void main(String[] argv)
{
DateFormat DFormat = DateFormat.getDateTimeInstance();
System.out.println( "Object: " + DFormat);
String str = DFormat.format( new Date());
System.out.println(str);
}
}
|
Output
Object: java.text.SimpleDateFormat@f95cf381
Jan 11, 2022, 6:25:40 AM
Example 2:
Java
import java.text.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
SimpleDateFormat SDFormat
= new SimpleDateFormat( "MM/dd/yyyy" );
Date date = new Date();
String str_Date1 = SDFormat.format(date);
System.out.println( "The Original: " + (str_Date1));
DateFormat DFormat = DateFormat.getDateTimeInstance();
System.out.println( "Object: " + DFormat);
String str = DFormat.format( new Date());
System.out.println(str);
}
}
|
Output
The Original: 01/11/2022
Object: java.text.SimpleDateFormat@f95cf381
Jan 11, 2022, 6:30:09 AM
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...