Java DateFormat getDateInstance() Example
DateFormat class getDateInstance() method example. This example shows you how to use getDateInstance() method.
Syntax is : public static final DateFormat getDateInstance(int style)
Method returns the date formatter with the given formatting style for the default locale.
Here is the code.
/**
* @(#) GetDateInstance1DateFormat.java
* A class representing use of method getDateInstance() of DateFormat
class in java.text Package.
* @Version 12-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class GetDateInstance1DateFormat {
public static void main(String[] args) {
// Create new Date object that will be initialized to the current date.
Date date = new Date();
System.out.println("Date by Date class : " + date.toString());
// Create new DateFormat object that will returns default date/time.
DateFormat dateFormat = DateFormat.getInstance();
// getDateInstance method call.
String d = dateFormat.getDateInstance(DateFormat.SHORT).format(date);
System.out.println("Date in SHORT style: " + d);
d = dateFormat.getDateInstance(DateFormat.LONG).format(date);
System.out.println("Date in LONG style: " + d);
d = dateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
System.out.println("Date in MEDIUM style: " + d);
d = dateFormat.getDateInstance(DateFormat.FULL).format(date);
System.out.println("Date in FULL style: " + d);
}
} |
Output of the program.
Date by Date class : Thu Jun 12 18:06:56 GMT+05:30 2008
Date in SHORT style: 6/12/08
Date in LONG style: June 12, 2008
Date in MEDIUM style: Jun 12, 2008
Date in FULL style: Thursday, June 12, 2008 |
|