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, Locale aLocale)
Method returns the date/time formatter with the given formatting styles for the given locale.
Here is the code.
/**
* @(#) GetDateTimeInstance2DateFormat.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 GetDateTimeInstance2DateFormat {
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();
Locale locale = new Locale("ENGLISH");
// getDateTimeInstance method call.
String dFormat = dateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM, locale).format(date);
System.out.println("1: Date by getDateTimeInstance : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat
.SHORT, locale).format(date);
System.out.println("2: Date by getDateTimeInstance : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat
.LONG, locale).format(date);
System.out.println("3: Date by getDateTimeInstance : " + dFormat);
dFormat = dateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat
.LONG, locale).format(date);
System.out.println("4: Date by getDateTimeInstance : " + dFormat);
}
} |
Output of the program.
Date by Date class : Thu Jun 12 18:17:01 GMT+05:30 2008
1: Date by getDateTimeInstance : 6/12/08 6:17:01 PM
2: Date by getDateTimeInstance : June 12, 2008 6:17 PM
3: Date by getDateTimeInstance : Jun 12, 2008 6:17:01 PM GMT+05:30
4: Date by getDateTimeInstance : Thursday, June 12, 2008 6:17:01 PM GMT+05:30
|
|