Java DateFormat getTimeInstance() Example
DateFormat class getTimeInstance() method example. This example shows you how to use getTimeInstance() method.
Syntax is : public static final DateFormat getTimeInstance(int style, Locale aLocale)
Method returns the time formatter with the given formatting style for the given locale.
Here is the code.
/**
* @(#) GetTimeInstance2DateFormat.java
* A class representing use of method getTimeInstance() of DateFormat
class in java.text Package.
* @Version 12-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class GetTimeInstance2DateFormat {
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 by getInstance method.
DateFormat dateFormat = DateFormat.getInstance();
String stringDate = dateFormat.format(date);
System.out.println("Date by DateFormat class : " + stringDate);
Locale locale = new Locale("ENGLISH");
// getTimeInstance method call.
String d;
d = dateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
System.out.println("SHORT style formatterfor specified locale : " + d);
d = dateFormat.getTimeInstance(DateFormat.LONG, locale).format(date);
System.out.println("LONG style formatterfor specified locale : " + d);
d = dateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(date);
System.out.println("MEDIUM style formatterfor specified locale : " + d);
d = dateFormat.getTimeInstance(DateFormat.FULL, locale).format(date);
System.out.println("FULL style formatterfor specified locale : " + d);
}
}
|
Output of the program.
Date by Date class : Thu Jun 12 18:26:28 GMT+05:30 2008
Date by DateFormat class : 6/12/08 6:26 PM
SHORT style formatterfor specified locale : 6:26 PM
LONG style formatterfor specified locale : 6:26:28 PM GMT+05:30
MEDIUM style formatterfor specified locale : 6:26:28 PM
FULL style formatterfor specified locale : 6:26:28 PM GMT+05:30 |
|