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()
Method returns the time formatter with the default formatting style for the default locale.
Here is the code.
/**
* @(#) GetTimeInstanceDateFormat.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 GetTimeInstanceDateFormat {
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);
// getTimeInstance method call.
String strDate = dateFormat.getTimeInstance().format(date);
System.out.println("Time formatter for the default locale. : "+strDate);
}
} |
Output of the program.
Date by Date class : Thu Jun 12 18:28:39 GMT+05:30 2008
Date by DateFormat class : 6/12/08 6:28 PM
Time formatter for the default locale. : 6:28:39 PM |
|