Java DateFormat getDateTimeInstance() Example
DateFormat class getDateTimeInstance() method example. This example shows you how to use getDateTimeInstance() method.
Syntax is : public static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Method returns the date/time formatter with the given date and time formatting styles for the default locale.
Here is the code.
/**
* @(#) GetDateTimeInstance1DateFormat.java
* A class representing use of method getDateTimeInstance() of DateFormat
class in java.text Package.
* @Version 12-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class GetDateTimeInstance1DateFormat {
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();
// getDateTimeInstance method call.
String dFormat = dateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM).format(date);
System.out.println("Date with SHORT MEDIUM style : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat
.SHORT).format(date);
System.out.println("Date with LONG SHORT style : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat
.LONG).format(date);
System.out.println("Date with MEDIUM LONG style : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat
.LONG).format(date);
System.out.println("Date with SHORT LONG style : " + dFormat);
}
} |
Output of the program.
Date by Date class : Thu Jun 12 18:14:48 GMT+05:30 2008
Date with SHORT MEDIUM style : 6/12/08 6:14:48 PM
Date with LONG SHORT style : June 12, 2008 6:14 PM
Date with MEDIUM LONG style : Jun 12, 2008 6:14:48 PM GMT+05:30
Date with SHORT LONG style : Thursday, June 12, 2008 6:14:48 PM GMT+05:30
|
|