Java DateFormat getInstance() Method with Examples
Last Updated :
16 Dec, 2022
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
import java.text.DateFormat;
import java.util.Date;
class Testclass {
public static void main(String[] args)
{
Date d = new Date();
DateFormat df = DateFormat.getInstance();
System.out.println( "DateFormat Object : " + df);
String str = df.format(d);
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
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class Testclass {
public static void main(String[] args)
{
Date d = new Date();
DateFormat df = DateFormat.getInstance();
System.out.println( "DateFormat Object : " + df);
String str = df.format(d);
System.out.println( "Current date : " + str);
SimpleDateFormat sdf
= new SimpleDateFormat( "MM-dd-yyyy" );
String st = sdf.format(d);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...